gpt4 book ai didi

c++ - 静态变量和模板特化歧义

转载 作者:太空狗 更新时间:2023-10-29 23:05:00 26 4
gpt4 key购买 nike

FQA提出这个代码示例:

#include <cstdio>
template<int n> struct confusing
{
static int q;
};
template<> struct confusing<1>
{
template<int n>
struct q
{
q(int x)
{
printf("Separated syntax and semantics.\n");
}
operator int () { return 0; }
};
};
char x;
int main()
{
int x = confusing< SOME_NUMBER_HERE >::q < 3 > (2);
return 0;
}

q 是指 static int qq(int x) 取决于 SOME_NUMBER_HERE 的值.考虑到 q 可以简单地命名为其他名称,这似乎是一个相当人为的示例。 gcc 甚至有一个警告:

warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

int x = confusing<2>::q < 3 > (2);

有没有实际场景会出现这种事情的问题?

最佳答案

我还没有看到,当然,这并不能证明有真实世界的例子。

我记得 Andrei Alexandrescu 曾就 static_if ( slides ) 发表过演讲。我认为他还解决了模板劫持问题,这与您的示例类似。

好吧,如果有人能想出一个真实的例子,我很确定 Andrei Alexandrescu 就是那个人。 :-)

关于c++ - 静态变量和模板特化歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20875690/

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