【问题标题】:Common syntax for declaring and initializing constexpr on MSVC and GCC在 MSVC 和 GCC 上声明和初始化 constexpr 的通用语法
【发布时间】:2016-07-03 19:47:45
【问题描述】:
namespace MyNS {
    template <>
    class Test<Test1> {
        public:
            constexpr static char const *description[] = { "X`", "Y1"};
        /*
        ...
        ...
        */
    } 
    constexpr char const * Test<Test1>::description[]; 
    /* Above definition is required when compiling with GCC but MSVC compiler gives error saying 'description' is redeclared.  */
    /* **Omitting definition of 'description', which is written outside class in namespace, causes successful compilation by MSVC but failure in GCC** */ 
}

是否有一种在 constexpr 之上定义、声明和初始化的通用方法,以便 MSVC 和 GCC 都能成功编译代码

【问题讨论】:

标签: c++


【解决方案1】:

这段代码:

#include <iostream>
namespace MyNS {
    template<class T> struct Test;
    template <>
    struct Test<int> {
        constexpr static char const * description[] = { "X1", "Y1"};
    };
}
int main() {
    std::cout << MyNS::Test<int>::description[0];
    return 0;
}

编译——据我所知——使用

  • g++-4.8+ -std=c++11
  • g++-4.8+ -std=c++1y
  • g++-4.9+ -std=c++14
  • g++-6.1+
  • g++-6.1+ -std=c++11
  • g++-6.1+ -std=c++14

没有进一步定义(其中4.8+ 表示从 g++ 4.8 版及更高版本开始)。

【讨论】:

  • 我很困惑。我不明白这如何回答问题Is there a common way to define, declare and initialize above constexpr such that code compiles successfully by both MSVC and GCC? 你有办法使用 MSVC 对其进行测试吗?
  • @jerry 看看 OP 谁说这个答案应该在 msvc 中工作
猜你喜欢
  • 2011-12-23
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-06
相关资源
最近更新 更多