2 default copy constructors: is it possible?

Is the following program ill-formed?

struct Foo
{
    Foo(Foo&) = default;

    Foo(const Foo&) = default;
};

int main() 
{
}

      

It compiles successfully with clang ++ 3.8.0 and g ++ 6.3.0 (compiler flags -std=c++11 -Wall -Wextra -Werror -pedantic-errors

).

+3


source to share


1 answer


Why should it be ill-formed? You define two copy constructors, one that expects a non-const argument and the other expects a const argument. Then you tell the compiler to use its default implementation for those two constructors. If the compiler has no reason to exclude the default copy constructors, you can also remove those two lines and get the same result. Also I think the first version is redundant since the default implementation should be ok with the const argument anyway. However, defining both is legal, since you can do something different in two cases.



+7


source







All Articles