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 q
或q(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);
这种情况是否存在实际应用场景,会造成问题?
SOME_NUMBER_HERE
!= 1代码也能编译通过。如果在足够大的代码库中,有无监督的模板爱好者在工作,我会感到惊讶如果这不成为一个真正的问题。 - IInspectable