Kevin Stallard
Kevin Stallard

Reputation: 109

Compiler misses invalid constructor call and calls non-existant (or private) default constructor

I have a constructor of a class that inherits from several other base classes:

Derived::Derived() :    MyRpc< Derived, DERIVED_RPC_CLASS_ID, true  > ( Derived::sServerName ),
                    MyEventSource<Derived>( *this ),
                    Smasher<Derived>( *this )

{ 
}

The default constructor of Smasher is being called. What is also strange, is that if I make the default constructor of Smash private or if I outright delete it, the compiler is still generating a public default constructor and calling it.

Here is how I have smasher defined:

class SmasherBase
{
    SmasherBase()=delete;
    SmasherBase( const char *name)
    {}
    .
    .
};

template< typename ... >  class Smasher : public SmasherBase
{
    Smasher()=delete;
};

template< typename LocalServerRPC_T >
class Smasher<LocalServerRPC_T> : public SmasherBase
{
public:
    Smasher()=delete;
    Smasher( LocalServerRPC_T &localServer, const char *name ) : SmasherBase( name ),
                                                                    mLocalServer( localServer )
    {
        ....
    }

    ~Smasher()
    {
    }

    typedef Smasher<LocalServerRPC_T>   Smasher_t;

protected:
    LocalServerRPC_T    &mLocalServer;
};

I would expect the compiler to complain that I am not calling an existing constructor as in:

error: no matching function for call to 'Smasher<Derived>::Smasher(EksoCAT&)' 

But it happily compiles the above code and then creates a default constructor over and above my objections. It also creates and calls a default constructor for the base class as well.

I have even tried making the default constructor private, hoping that it would at least realize that it shouldn't create a compiler generated default constructor in its place and perhaps complain that the constructor is private.

No such luck. The code is happily generated and when I run, and I can step into the phantom default constructor.

I am using the GNU 4.8.3 C++ compiler. I have also cleaned, rebuilt, searched for old versions of these files to make sure they weren't being used, etc, etc to no avail.

Thank you all for any help and ideas.

Upvotes: 0

Views: 63

Answers (1)

Mark B
Mark B

Reputation: 96241

My psychic debugging skills tell me that your class Derived inherits from Smasher<Derived>. In that case, your call to Smasher<Derived>(*this) is in fact calling Smasher's copy constructor which you haven't deleted or defined yourself, so the compiler will happily generate one for you. The debugger is almost certainly simply not showing you the exact constructor being called.

Upvotes: 2

Related Questions