如果模板类型是可实例化的,则执行某些操作
如果我可以实例化某个模板类,我想使用 SFINAE 模式来执行一些代码。让我们想象一下:
//Only instantiable with types T for which T.x() is ok:
template <class T>
class TemplateClass
{
T t;
public:
void foo() {
t.x();
}
}
template <class T>
class User
{
void foo()
{
"if TemplateClass<T> is ok then do something else do nothing"
}
}
我怎样才能做到这一点?
多谢!
编辑:根据 edA-qa mort-ora-y 的回答,我尝试过:
template <class T>
struct TemplateClass
{
T t;
void foo() { t.x(); }
static const bool value = true;
};
struct A {};
struct B { void x() {} };
template <class T>
struct User
{
template<typename M>
typename boost::enable_if<TemplateClass<M> >::type func( )
{
std::cout << "enabled\n";
}
template<typename M>
typename boost::disable_if<TemplateClass<M> >::type func( )
{
std::cout << "disabled\n";
}
void foo()
{
func<TemplateClass<T> >();
}
};
User<A> a;
a.foo();
User<B> b;
b.foo();
但这返回“启用启用”。我缺少什么?
I would like to use the SFINAE pattern to execute some code if I can instantiate a certain template class. Let's imagine this:
//Only instantiable with types T for which T.x() is ok:
template <class T>
class TemplateClass
{
T t;
public:
void foo() {
t.x();
}
}
template <class T>
class User
{
void foo()
{
"if TemplateClass<T> is ok then do something else do nothing"
}
}
How could I do that?
Thanks a lot!
EDIT: Based on edA-qa mort-ora-y's answer I tried:
template <class T>
struct TemplateClass
{
T t;
void foo() { t.x(); }
static const bool value = true;
};
struct A {};
struct B { void x() {} };
template <class T>
struct User
{
template<typename M>
typename boost::enable_if<TemplateClass<M> >::type func( )
{
std::cout << "enabled\n";
}
template<typename M>
typename boost::disable_if<TemplateClass<M> >::type func( )
{
std::cout << "disabled\n";
}
void foo()
{
func<TemplateClass<T> >();
}
};
User<A> a;
a.foo();
User<B> b;
b.foo();
But this returns "enabled enabled". What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该查看 boost boost/utility/enable_if.hpp 标头和关联的元/模板编程代码。
这里最简单的方法是拥有两个版本的 foo 函数,都是模板函数。其中一个函数将使用enable_if 构造,另一个函数将使用disable_if 构造。
我确信您可以在 boost 网站上找到更好的示例,但类似这样:
只有当 Template 是有效类型时,才会定义此函数。由于您总是想要编译,因此您需要对应的函数,即无效时要调用的函数:
我不确定您是否可以在单个模板中在此模式中定义两个成员函数,而不使它们都是模板函数。我想您也许能够定义两个模板函数并将默认模板参数设置为 T。
我希望这会有所帮助。
You should look at the boost boost/utility/enable_if.hpp header and the associated meta/template programming code.
The easiest way here is to have two versions of the foo function, both template functions. One of the functions will use the enable_if construct and the other will use the disable_if construct.
I'm sure you can find better examples at the boost website, but something like this:
This function will only be defined if Template is a valid type. Since you always want to compile you'll need the counterpart, the function to call when it is not valid:
I'm not sure you can, within a single template, define two member functions in this pattern without making them both template functions. I suppose you might be able to define the two template functions and set the default template parameter to T.
I hope that helps a bit.