静态变量和模板特化歧义

时间:2014-01-02 03:04:39

标签: c++

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);

是否存在这种问题的实际情况?

1 个答案:

答案 0 :(得分:1)

我没有看到任何东西,当然,这并不能证明存在真实世界的例子。

我记得Andrei Alexandrescu谈到了static_if(slides)。我认为他也解决了模板劫持问题,这与你的例子类似。

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

相关问题