Search code examples
templatesvisual-c++c++11friendclang++

Issue with friend template functions clang++ / msvc++ and enable_if


I get a compiler error in clang++. MSVC++ is happy. I believe my declarations are correct.

Am I incorrect in my beliefs and I am "lucky" in MSVC? Is there a non #ifndef _MSC_VER ... public: way to make this work in both compilers?

I'd like to keep the constructor private. The real code is slightly more complex. (additional template meta-programming and "perfect forwarding") The following is a distilled version for the question and to isolate the issue as much as possible. I've tried a number of variations for the friend declaration. The one that "seems best" is shown in the example.

#include<type_traits>

template<typename T> class Wrap;

template<typename T, 
    typename std::enable_if<std::is_class<T>::value, T>::type* = nullptr >
Wrap<T> make_wrapper( T i )
{
    return Wrap<T>( i );
}

template<typename T>
class Wrap : T
{
    friend Wrap<T> make_wrapper<T,nullptr>( T );
private:
    Wrap( T v ) : T( v ) {}
};

template<typename T>
class Imp
{
    T x;
public:
    Imp( T v ) {}
};

int main()
{
    auto wrap = make_wrapper( Imp<int>( 1 ) );
    return 0;
}

clang++:

$ clang++ --version
Debian clang version 3.5-1~exp1 (trunk) (based on LLVM 3.5)
Target: x86_64-pc-linux-gnu
Thread model: posix

$ clang++ -std=c++11 test.cpp
test.cpp:8 : 12 : error : calling a private constructor of class 'Wrap<Imp<int> >'
return Wrap<T>( i );
^
test.cpp:30 : 17 : note : in instantiation of function template specialization 'make_wrapper<Imp<int>, nullptr>' requested here
auto wrap = make_wrapper( Imp<int>( 1 ) );
^
test.cpp:16 : 5 : note : declared private here
Wrap( T v ) : T( v ) { }
^
1 error generated.

cl

Microsoft Visual Studio Professional 2013
Version 12.0.30501.00 Update 2

1>------ Build started: Project: Test1, Configuration: Debug Win32 ------
1>  Test1.cpp
1>  Test1.vcxproj -> C:\cygwin64\home\username\Test1\Debug\Test1.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Solution

  • Template friends are notoriously complicated. I don't know for sure whether Clang is right, but it could be that your SFINAE trick inside the function template arguments runs afoul of

    14.5.4 Friends [temp.friend]

    9 When a friend declaration refers to a specialization of a function template, the function parameter declarations shall not include default arguments, nor shall the inline specifier be used in such a declaration.

    C++11 introduced default template arguments for function templates, and it could be that clang interprets the above differently as g++/MSVC. It is fixable by doing SFINAE on the return type instead:

    #include<type_traits>
    
    template<typename T> class Wrap;
    
    template<typename T>
    using WrapRet = typename std::enable_if<std::is_class<T>::value, Wrap<T>>::type;
    
    template<typename T>
    WrapRet<T> make_wrapper( T i )
    {
        return Wrap<T>( i );
    }
    
    template<typename T>
    class Wrap : T
    {
        friend WrapRet<T> make_wrapper<T>( T );
    private:
        Wrap( T v ) : T( v ) {}
    };
    
    template<typename T>
    class Imp
    {
        T x;
    public:
        Imp( T v ) {}
    };
    
    int main()
    {
        auto wrap = make_wrapper( Imp<int>( 1 ) );
        return 0;
    }
    

    Live Example that works with both clang and g++.