#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);
}
Run Code Online (Sandbox Code Playgroud)
大家好.
我正在使用变量"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).