在MSVC和GCC上声明和初始化constexpr的通用语法
Common syntax for declaring and initializing constexpr on MSVC and GCC
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下都能成功编译?
代码:
#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开始及以后)。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 多成员Constexpr结构初始化
- 条件constexpr函数
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- Visual C++ constexpr Hints
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 为什么constexpr的性能比正常表达式差
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 为什么std::isnan 不是 constexpr?
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 更多constexpr容器是否需要mark_immutable_if_consexpr
- 在MSVC和GCC上声明和初始化constexpr的通用语法