真的,在“香草” C++你无论是用手写,还是用特定的类型,或者使用像Dirkgently建议的模板。
这就是说,如果你可以使用升压这个你想要做什么:
template <class T>
StreamLogger& operator<<(T val)
{
typedef boost::mpl::vector<const char*, int,
unsigned, size_t> allowed_types;
BOOST_MPL_ASSERT_MSG(boost::mpl::contains<allowed_types, T>::value,
TYPE_NOT_ALLOWED, allowed_types);
// generic implementation follows
elements.push_back(boost::lexical_cast<std::string>(val));
return *this;
}
如果被编译的类型没有在包含这将产生内嵌有消息TYPE_NOT_ALLOWED
编译时错误类型串。
此外,由于此答案需要Boost我只使用lexical_cast
。您会注意到您正在重复该代码,这很糟糕。考虑将wrapping这个功能变成一个函数。
如果您不能使用Boost,你可以用某种特质很容易地模拟这样的:如果断言失败
template <typename T, typename U>
struct is_same
{
static const bool value = false;
};
template <typename T>
struct is_same<T, T>
{
static const bool value = true;
};
template <bool>
struct static_assert;
template <>
struct static_assert<true> {}; // only true is defined
// not the best, but it works
#define STATIC_ASSERT(x) static_assert< (x) > _static_assert_
template <class T>
StreamLogger& operator<<(T val)
{
STATIC_ASSERT(is_same<const char*, T>::value ||
is_same<int, T>::value ||
is_same<unsigned, T>::value ||
is_same<size_t, T>::value);
// generic implementation follows
elements.push_back(boost::lexical_cast<std::string>(val));
return *this;
}
这也将产生一个编译时错误,尽管代码不如性感。 :(< - 不性感
@anon:你想只为这些类型或它可以接受的其他类型吗?换句话说,如果您尝试使用其他类型应该发生什么?编译器错误或不 - op或push_back()插入向量? – Naveen 2010-02-24 05:13:15
重复:http://stackoverflow.com/questions/148373/c-restrict-template-function – Clifford 2010-02-24 05:32:36
@Clifford:但是没有一个像我的解决方案一样性感。:) – GManNickG 2010-02-24 05:34:05