问题
I've created a header for optionally-lazy parameters (also visible in a GitHub repository).
In my original version of the code, I provided a protected default constructor for my base-class template:
template <typename VAL_TYPE>
class LazyType_Base
{
// ....
LazyType_Base(void) =default;
// ....
Then, in one of the derived classes:
template <typename VAL_TYPE>
class LazyType_Eager : public LazyType_Base<VAL_TYPE>
{
public:
LazyType_Eager(
VAL_TYPE&& final_val)
: LazyType_Base<VAL_TYPE>{}
, val_{final_val}
{}
// .....
This compiles just fine with Clang++, but in G++ 5.1, I get this error:
In file included from Test_OptionallyLazy.cpp:3:0:
OptionallyLazy.hpp: In instantiation of ‘LazyType_Eager<VAL_TYPE>::LazyType_Eager(VAL_TYPE&&) [with VAL_TYPE = int]’:
Test_OptionallyLazy.cpp:22:14: required from here
OptionallyLazy.hpp:23:5: error: ‘LazyType_Base<VAL_TYPE>::LazyType_Base() [with VAL_TYPE = int]’ is protected
LazyType_Base(void) =default;
^
OptionallyLazy.hpp:58:23: error: within this context
, val_{final_val}
What's going on here? The weirdest bit is that the other derived class does something similar that doesn't trigger an error.
Replacing the =default
constructor with the explicit default implementation {}
resolves the compiler error.
EDIT: Thanks to T.C., here is a real MCVE:
class Meow
{
protected:
Meow(void) =default;
public:
virtual void f() {}
};
class Purr : public Meow
{
public:
Purr()
: Meow{}
{}
};
来源:https://stackoverflow.com/questions/38213809/g-doesnt-permit-use-of-protected-default-constructor-in-base-class-when-both