【发布时间】:2019-05-19 08:38:46
【问题描述】:
#include <iostream>
using namespace std;
template<bool enable, typename T>
struct foo;
template<typename T>
struct foo<false , T>
{
//nothing
};
template<typename T>
struct foo<true , T>
{
void say_hello()
{
cout << "Hello !" << endl;
}
protected:
int m_some_data_when_I_enabled{};
};
template<bool Enable, typename T>
struct bar
:
foo<Enable , T>
{
//And there are lots of functions and members
//Here I need conditional 'using'
using foo<Enable , T>::say_hello;
void say_hello(int different_signature)
{
}
};
struct duck { };
int main(int, char**) {
bar<true , duck> enabled_bar;
bar<false , duck> disabled_bar;
}
当我声明 bar 时它给出了一个错误。对于我,这说得通。所以我需要类似的东西:
template<typename = typename std::enable_if<Enable>::type>
using foo<Enable , T>::say_hello();
我知道我可以通过专门化 'bar' 来解决问题,但它有一些成员,在这种情况下我会复制很多代码。有没有不同的,也许是棘手的方式?
【问题讨论】:
标签: c++ metaprogramming generic-programming