【发布时间】:2010-07-24 09:30:27
【问题描述】:
出于某种复杂的原因,我想将任何支持的类型 T(来自模板)转换为我选择的类型列表。为此,我尝试使用名为“Convert”的模板结构。例如:
Convert<short>::type should be int
Convert<int>::type should be int
Convert<char>::type should be int
Convert<float>::type should be double
Convert<double>::type should be double
Convert<const char*>::type should be std::string
Convert<std::string>::type should be std::string
etc.
使用模板专业化很容易实现上述那些。但是有一种情况会导致问题:
Convert<T>::type where T is a functor should be T
要处理这个问题,我想我必须使用SFINAE,但我无法让它编译。
下面的代码给了我“partial specialization cannot match argument list for primary template”(即禁止写“Convert”):
template<typename T, typename = decltype(&T::operator())>
struct Convert<T> { typedef T type; };
而这个给了我“template parameter not used or deducible in partial specialization”(即它认为没有使用 T):
template<typename T>
struct Convert<typename std::enable_if<std::is_function<typename T::operator()>::value,T>::type>
{ typedef T type; };
我不知道该怎么做,我的所有尝试都导致上述两个错误之一。
编辑:我想用相同的模型捕捉其他通用的东西,所以我不能只在非专业结构中写“typedef T type”。
谢谢
【问题讨论】:
-
你是什么意思“抓住其他通用的东西”?
-
您需要澄清“函子”的含义。功能也不错吗?或者它只是功能对象?如果是这样,那么
operator()是否过载?还是源自std::function_object?特定数量的参数? -
示例:"Convert
::type is int if T is convertible to an int", or "Convert ::type is ... if T has a begin and an end功能”等 -
@sbi:在上面的示例中,我尝试使用 operator()(任意数量的参数)来捕获函数对象;我可以处理其他专业的功能
-
@Tomaka17:你见过这个吗:stackoverflow.com/questions/257288/…? (链接已更改)