【发布时间】:2017-04-19 10:32:21
【问题描述】:
我想知道模板类型是否有“push_back”方法。 我试过这个例子:Is it possible to write a template to check for a function's existence?
我的代码:
template <typename T>
class has_push_back
{
typedef char one;
typedef long two;
template <typename C> static one test(char[sizeof(&C::push_back)]);
template <typename C> static two test(...);
public:
enum { value = (sizeof(test<T>(0)) == sizeof(char)) };
};
我的电话:
template <typename T>
HTTPMessage Serializer::GetHTTPMessage(T varToSerialize)
{
std::cout << has_push_back<T>::value << std::endl;
//some code
}
不幸的是,当使用 std::string 调用 GetHTTPMessage 时出现此错误:
'overloaded-function': 非法 sizeof 操作数
注意:参见类模板实例化“has_push_back”的参考 正在编译 和 [ T=std::字符串 ]
我不明白为什么它不能编译。
【问题讨论】: