gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:02:55 28 4
gpt4 key购买 nike

#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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com