clang++ error on late default template parameter declaration

The following code compiles fine with g++, but not with clang++ (3.6):

// Forward declaration:
template <class S, class T>
struct Base;

template <class T>
struct BaseFriend {
    friend struct Base<int, T>;
};

// Actual declaration:
template <class S, class T = int>
struct Base {
    void foo() {}
};

struct DerivedFriend : BaseFriend<int> {};

struct Derived : Base<int> {
    void foo(int) {
        Base<int>::foo();
    }
};

Error occurs in the Derived::foo definition:

error: too few template arguments for class template 'Base'
    Base<int>::foo();
    ^
test.cpp:3:8: note: template is declared here
struct Base;
       ^

Error goes away after few minor fixes, like:

  • If default template parameter is defined in forward declaration instead of actual declaration.
  • Or if DerivedFriend is not used.
  • But, what is wrong with the original code?


    Definitely a clang bug, looks like #10147. The standard clearly allows this [temp.param]/10:

    The set of default template-arguments available for use with a template declaration or definition is obtained by merging the default arguments from the definition (if in scope) and all declarations in scope in the same way default function arguments are (8.3.6). [ Example:

    template<class T1, class T2 = int> class A;
    template<class T1 = int, class T2> class A;
    

    is equivalent to

    template<class T1 = int, class T2 = int> class A;
    

    —end example ]

    链接地址: http://www.djcxy.com/p/84576.html

    上一篇: 在共享文件夹中打开并保存本地Intranet站点上的文档

    下一篇: 在延迟默认模板参数声明中出现clang ++错误