作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
#include <iostream>
struct Index {
constexpr operator int() const { return 666; }
};
template <int i> void foo() {
std::cout << i << std::endl;
}
void wrapper(Index index) {
foo<index>();
}
int main() {
Index index;
// foo<index>(); // error: the value of ‘index’ is not usable in a constant expression
wrapper(index);
}
大家好。
我正在使用 constexpr 将变量“索引”转换为 int
值,它被替换为“foo”模板函数。如果我直接调用foo<index>()
从“main”,我得到一个编译器错误。如果从“包装器”执行相同的调用,那么一切都可以编译并正常工作。
我错过了什么?
编译命令:g++ -std=c++14 main.tex
使用 g++ (GCC) 5.3.1 20160406 (Red Hat 5.3.1-6)。
最佳答案
报告了一个非常相似的错误 here .它首先在 GCC 4.9.0 中被报道
在提供的分析中:
This is a GCC bug. It appears to be some sort of confusion in the way GCC handles internal linkage non-type template parameters of pointer type.
问题已经解决了。
关于c++ - 为什么 constexpr 隐式转换并不总是有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43405876/
我是一名优秀的程序员,十分优秀!