[cfe-dev] early instantiation of defaulted copy constructor

Nathan Ridge zeratul976 at hotmail.com
Sun Apr 29 11:59:18 PDT 2012


Hello,

The following code fails to compile with Clang trunk:

template <class T, class U>
struct pair
{
    T first;                 
    U second;                

    pair() : first(), second() {}

    pair(const pair&) = default;
};

struct S
{
    S() {}
    S(const S&) = delete;
    S(S&&) = default;
};

int main()
{
    pair<int, S> p;
}

The errors are:

test.cpp:9:5: error: the parameter for this explicitly-defaulted copy constructor is const, but a member or base requires it to be
      non-const
    pair(const pair&) = default;
    ^
test.cpp:21:18: note: in instantiation of template class 'pair<int, S>' requested here
    pair<int, S> p;
                 ^

Why is clang trying to instantiate the copy constructor when it's never used?

Is there something about the copy constructor being defaulted that is causing
early instantiation? If I replace the defaulted copy constructor with an
explicit implementation:

pair(const pair& p) : first(p.first), second(p.second) {}

then I get no error, suggesting that clang is not trying to instantiate the
copy constructor.

As far as I'm aware, the defaulted implementation and the explicit
implementation should be the same in all respects, including time of
instantiation.

GCC compiles the original code without errors.

Thanks,
Nate
 		 	   		  



More information about the cfe-dev mailing list