实例化模板时短路?
考虑这个代码片段,
template<bool b>
struct other
{
static const bool value = !b;
};
template<bool b>
struct test
{
static const bool value = b || other<b>::value;
};
int main()
{
bool value = test<true>::value;
}
当实例化似乎完全没有必要时,编译器是否会在上述情况下实例化 other
?或者仅仅因为我编写了语法 other::value
,编译器就必须实例化它,无论它对 test
?
我想听听,a)标准要求什么,以及b)各种编译器实际实现了什么?标准中的相关部分将不胜感激。
Consider this code snippet,
template<bool b>
struct other
{
static const bool value = !b;
};
template<bool b>
struct test
{
static const bool value = b || other<b>::value;
};
int main()
{
bool value = test<true>::value;
}
Do compilers instantiate other<true>
in situations such as the above, when instantiating seems completely unnecessary? Or just because I've written the syntax other<b>::value
, compilers must instantiate it regardless of the fact that it contributes absolutely nothing to the calculation of the value of test<true>::value
?
I would like to hear, a) what is required by the Standard, and b) what is actually implemented by the various compilers? Relevant sections from the Standard would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 C++ 规范,$14.7.1/4 节:
的情况下,该类必须具有完整的类型,因为您要在其内部查找值静态成员。这会阻止编译器短路表达式
。对于实践中实际发生的情况,我不确定,因为我看不出编译器如何能够不进行实例化,例如,假设
other
的实例化看起来是这样。像这样:在这里,您的程序格式不正确,因为
other
::value 是一种类型,而不是值,但编译器在不实际进行实例化的情况下无法诊断错误。According to the C++ spec, section $14.7.1/4:
In the case you illustrated with short-circuiting, the class would have to have a complete type, because you're looking inside of it to find the value static member. This precludes the compiler from short-circuiting the expression.
As for what actually happens in practice, I'm not sure because I can't see how the compiler could get away with not doing the instantiation. For example, suppose that the instantiation of
other<b>
looked like this:Here, your program would be ill-formed because
other<b>
::value is a type, not a value, but the compiler couldn't diagnose the error without actually doing the instantiation.请注意,自从 C++11 中引入了
constexpr
函数,您就可以实现短路:... 虽然
test_other()
未定义,但编译器不会尝试来调用它。不幸的是,这不适用于 consteval。在这种情况下,第一级“测试工具”
test
本身必须是一个函数:Note that since the introduction of
constexpr
functions in C++11, you can achieve short-circuiting:... though
test_other()
is undefined the compiler doesn't attempt to call it.Unfortunately this doesn't work with
consteval
. In this case the first-level 'test facility'test
itself must be a function: