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

时间:2017-04-14 05:46:53

标签: 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);
}

大家好。

我使用constexpr转换变量&#34; index&#34;到int值,替换为&#34; foo&#34;模板化的功能。 如果我直接从&#34; main&#34;中调用foo<index>(),我会收到编译错误。 如果从&#34;包装器&#34;完成相同的调用,那么一切都可以编译并正常工作。

我在那里错过了什么?

编译命令:g++ -std=c++14 main.tex与g ++(GCC)5.3.1 20160406(Red Hat 5.3.1-6)。

1 个答案:

答案 0 :(得分:0)

报告了一个非常相似的错误here。最初是在GCC 4.9.0中报道的

在提供的分析中:

  

这是GCC错误。 GCC处理指针类型的内部链接非类型模板参数的方式似乎有些混乱。

此后已解决。

相关问题