为什么constexpr隐式转换并不总是有效?

Dr.*_*erg 13 c++ casting g++

#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).

P.W*_*P.W 0

这里报告了一个非常相似的错误。首次在 GCC 4.9.0 中报告

在提供的分析中:

这是一个 GCC 错误。GCC 处理指针类型的内部链接非类型模板参数的方式似乎存在某种混乱。

此后问题已得到解决。