vendredi 1 avril 2016

Does "= default" allow out-of-line implementations?

Usually I see the = default syntax used in the header. My understanding is that this is the same as if the functions are explicitly implemented in the header, see Foo below.

Foo.h

#pragma once

class Foo
{
public:
    Foo() = default;

    Foo(const Foo& other) = default;
};

Purely out of curiosity, can the = default be used in the source file as follows?

Bar.h

#pragma once

class Bar
{
public:
    Bar();

    Bar(const Bar& other);
};

Bar.cpp

#include "Bar.h"

Bar::Bar() = default;

Bar::Bar(const Bar&) = default;

As far as I know this is equivalent to explicitly implementing the functions in the source file.

The above Bar example compiles with gcc-5.1 but does the standard allow for this usage? As an aside, are there any benefits to using = default in the source file versus the header?

Aucun commentaire:

Enregistrer un commentaire