我试图了解std :: enable_if如何处理inn模板参数。
#include <type_traits>
#include <iostream>
#include <memory>
using namespace std;
class Interface {};
class Value {};
class Stream
{
public:
template<typename T,
typename enable_if<
is_base_of< Interface, T>{}
>::type* = nullptr>
void
write(const shared_ptr<T>& v)
{
cerr << "Writing interface class" << endl;
}
template<typename T,
typename enable_if<
is_base_of< Value, T>{}
>::type* = nullptr>
void
write(const shared_ptr<T>& v)
{
cerr << "Writing value class" << endl;
}
};
class UserI : public Interface{};
class User : public Value{};
int main(int, char**)
{
auto interface = make_shared<UserI>();
auto value = make_shared<User>();
Stream s;
s.write(interface);
s.write(value);
return 0;
}
我试图通过提供自定义特征来检查对象是接口还是值实例来简化代码。
#include <type_traits>
#include <iostream>
#include <memory>
using namespace std;
class Interface {};
class Value {};
template<typename T>
struct is_interface
{
const static bool value = is_base_of< Interface, T>::value;
};
template<typename T>
struct is_value
{
const static bool value = is_base_of< Value, T>::value;
};
class Stream
{
public:
template<typename T,
typename enable_if<
is_interface<T>{}
>::type* = nullptr>
void
write(const shared_ptr<T>& v)
{
cerr << "Writing interface class" << endl;
}
template<typename T,
typename enable_if<
is_value<T>{}
>::type* = nullptr>
void
write(const shared_ptr<T>& v)
{
cerr << "Writing value class" << endl;
}
};
class UserI : public Interface{};
class User : public Value{};
int main(int, char**)
{
auto interface = make_shared<UserI>();
auto value = make_shared<User>();
Stream s;
s.write(interface);
s.write(value);
return 0;
}
但第二个版本无法编译,出现以下错误:
test_t2.cc: In function ‘int main(int, char**)’:
test_t2.cc:58:26: error: no matching function for call to ‘Stream::write(std::shared_ptr<UserI>&)’
s.write(interface);
^
test_t2.cc:58:26: note: candidates are:
test_t2.cc:32:9: note: template<class T, typename std::enable_if<is_interface<T>{}>::type* <anonymous> > void Stream::write(const std::shared_ptr<_Tp1>&)
write(const shared_ptr<T>& v)
^
test_t2.cc:32:9: note: template argument deduction/substitution failed:
test_t2.cc:30:28: error: could not convert template argument ‘is_interface<UserI>{}’ to ‘bool’
>::type* = nullptr>
有人可以解释第二个版本的问题是什么吗?
答案 0 :(得分:3)
typename enable_if<
is_base_of< Interface, T>{}
>::type* = nullptr
这是有效的,因为std::is_base_of
提供operator bool
隐式将其实例转换为bool
。您的特征不提供此特征,因此使用is_interface<T>{}
作为bool
无效。
您可以为自己的特征撰写operator bool
,但最简单的方法是使用::value
代替:
template<typename T,
typename enable_if<
is_interface<T>::value //here
>::type* = nullptr>
void
write(const shared_ptr<T>& v)
就个人而言,我认为在您的情况下,标签调度方法会更清晰,更易于维护。这是一个可能的实现:
class Stream
{
private:
struct interface_tag{};
struct value_tag{};
//if you ever need more tags, add them here
struct invalid_tag{};
template <typename T>
struct get_tag {
static interface_tag tagger (Interface*);
static value_tag tagger (Value*);
//add any mappings for other tags
static invalid_tag tagger (...);
using tag = decltype(tagger(std::declval<T*>()));
};
//convenience alias
template <typename T> using tag_t = typename get_tag<T>::tag;
public:
//clean public interface
template <typename T>
void write (const shared_ptr<T>& v) {
write(v, tag_t<T>{});
}
private:
//no more horrible std::enable_if
template<typename T>
void
write(const shared_ptr<T>& v, interface_tag)
{
cerr << "Writing interface class" << endl;
}
template<typename T>
void
write(const shared_ptr<T>& v, value_tag)
{
cerr << "Writing value class" << endl;
}
};
如果您需要澄清此方法,请询问。