我创建了一个可选惰性参数的标头 https://codereview.stackexchange.com/q/133991/110660(也可见于GitHub 存储库 https://github.com/BatmanAoD/PublicPaste/tree/master/GenericCppCode/OptionalLaziness).
在我的原始代码版本中,我为我的基类模板提供了一个受保护的默认构造函数:
template <typename VAL_TYPE>
class LazyType_Base
{
// ....
LazyType_Base(void) =default;
// ....
然后,在派生类之一中:
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}
{}
// .....
这在 Clang++ 中编译得很好,但在 G++ 5.1 中,我收到此错误:
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}
这里发生了什么?最奇怪的是另一个派生类做了类似的事情doesn't触发错误。
更换=default
具有显式默认实现的构造函数{}
解决编译器错误。
EDIT:感谢 T.C.,here http://melpon.org/wandbox/permlink/J8sHbWHYFQ26hHZq是一个真正的 MCVE:
class Meow
{
protected:
Meow(void) =default;
public:
virtual void f() {}
};
class Purr : public Meow
{
public:
Purr()
: Meow{}
{}
};