模板类中的变量模板-意外错误(可能存在错误?)
Variable template in template class - unexpected error (possible bug?)
拥有:
struct Value
{
template<class T>
static constexpr T value{0};
};
(0(表意
template<typename TValue>
struct Something
{
void x()
{
static_assert(TValue::template value<int> == 0, "");
}
};
int main() { Something<Value>{}.x(); return 0; }
不使用clang++3.6进行编译。
错误:不能引用没有模板参数列表的变量模板"value">
不使用g++5.2进行编译。
错误:"template constexpr const T Value::Value"不是函数模板
(1(表意
同时使用clang++和g++进行编译。
struct Something
{
void x()
{
static_assert(Value::template value<int> == 0, "");
}
};
int main() { Something{}.x(); return 0; }
为什么(0(编译失败?
如果通过模板参数(在本例中为TValue
(访问变量模板,则似乎会出现问题。为TValue
定义类型别名或使用typename
关键字并不能解决此问题。
这是怎么回事?
在将变量模板作为依赖名称进行处理时,这无疑是gcc和clang的一个bug。我提交了gcc 67248和clang 24473。
作为目前的解决方法,两个编译器都支持旧的变量模板方式,即如果您添加:
struct Value
{
template<class T>
static constexpr T value = 0;
template <typename T>
struct variable_template_ish {
static constexpr T value = Value::value<T>;
};
};
然后编译以下内容:
template<typename TValue>
struct Something
{
void foo() {
static_assert(TValue::template variable_template_ish<int>::value == 0, "");
}
};
int main() {
Something<Value>{}.foo();
}
我以前在c++中创建模板类头文件时遇到过一些麻烦。
确保static constexpr T value{0};
的实现与声明位于同一个头文件中。
相关文章:
- 为什么此代码存在运行时错误?
- 错误:令牌 { '{' 之前应存在非限定 ID
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- Visual Studio 2019在内置库中存在错误
- C++为什么我的指针选择排序中存在分段错误?
- MSVC 中可能存在的编译器错误
- Microsoft Visual Studio 2019交叉编译CMake库 - 有效,但存在SEG错误
- 在 Windows 10 下使用 GTest 的 CMake - 致命错误 LNK1104:无法打开文件"gtest.lib",但存在调试"gtestd.lib"
- 为什么此代码中存在分段错误?
- 无法在这个基本的Qt代码片段中找到错误,但我被告知它肯定存在?
- 由于相同的文件名/类名存在于不同的SO中而导致的SEG错误
- 以下 OpenCV 源代码中是否存在错误?(#ifdef 没有 #else)
- 带有unique_ptr的通用单链表,MS visual studio C++中存在未知错误
- 指向类成员函数的指针中存在类型转换错误
- 使用c++的gSOAP中的stdsoap2.cpp中存在编译错误
- C++错误:'else'之前应存在非限定 ID
- 这是 GCC 中可能存在的错误吗?
- 我正在调试这个C++程序.编译器不再显示语法错误,但存在隐藏的逻辑错误
- 当错误存在时,为什么我的程序不执行第二个catch块