我正在试验 C++11 的新特性。在我的设置中,我真的很想使用继承构造函数,但不幸的是还没有编译器实现这些。因此,我试图模拟相同的行为。我可以这样写:
template <class T>
class Wrapper : public T {
public:
template <typename... As>
Wrapper(As && ... as) : T { std::forward<As>(as)... } { }
// ... nice additions to T ...
};
这很有效……大多数时候。有时代码使用 Wrapper
类必须使用 SFINAE 来检测这样的 Wrapper<T>
可以构建。但是存在以下问题:就重载决议而言,Wrapper<T>
的构造函数将接受任何参数 - 但如果类型为 T
,则编译失败(这 不 SFINAE 涵盖)不能用这些来构造。
我尝试使用 enable_if
有条件地启用构造函数模板的不同实例化。
template <typename... As, typename std::enable_if<std::is_constructible<T, As && ...>::value, int>::type = 0>
Wrapper(As && ... as) // ...
只要满足以下条件就可以正常工作:
T
的适当构造函数是public
-
T
不是抽象的
我的问题是:如何摆脱上述两个约束?
我试图通过检查(使用 SFINAE 和 sizeof()
)是否表达式 new T(std::declval<As &&>()...)
来克服第一个问题。格式正确在 Wrapper<T>
.但这当然行不通,因为派生类可以使用其基类的 protected 构造函数的唯一方法是在成员初始化列表中。
对于第二个,我完全不知道——它是我需要的更多,因为有时它是 Wrapper
实现了T
的抽象函数,使其成为一个完整的类型。
我想要一个解决方案:
- 按标准正确
- 适用于任何 gcc-4.6.*、gcc-4.7.* 或 clang-3.*
谢谢!
最佳答案
这似乎在我的本地 GCC(4.7,由 rubenvb 提供)上运行良好。不过,ideone 上的 GCC 会打印几个“已实现”的编译器内部错误。
我不得不公开 Experiment
类的“实现细节”,因为出于某些原因(这听起来像一个错误),我的 GCC 版本提示它们是私有(private)的,即使只有类本身使用它。
#include <utility>
template<typename T, typename Ignored>
struct Ignore { typedef T type; };
struct EatAll {
template<typename ...T>
EatAll(T&&...) {}
};
template<typename T>
struct Experiment : T {
public:
typedef char yes[1];
typedef char no[2];
static void check1(T const&);
static void check1(EatAll);
// if this SFINAE fails, T accepts it
template<typename ...U>
static auto check(int, U&&...u)
-> typename Ignore<no&,
decltype(Experiment::check1({std::forward<U>(u)...}))>::type;
template<typename ...U>
static yes &check(long, U&&...);
public:
void f() {}
template<typename ...U,
typename std::enable_if<
std::is_same<decltype(Experiment::check(0, std::declval<U>()...)),
yes&>::value, int>::type = 0>
Experiment(U &&...u):T{ std::forward<U>(u)... }
{}
};
// TEST
struct AbstractBase {
protected:
AbstractBase(int, float);
virtual void f() = 0;
};
struct Annoyer { Annoyer(int); };
void x(Experiment<AbstractBase>);
void x(Annoyer);
int main() {
x({42});
x({42, 43.f});
}
更新:该代码也适用于 Clang。
关于c++ - 检测(可能是抽象的)基类的 protected 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12079711/