Mic*_*uza 0 c++ templates sfinae visual-studio-2013
我有这段代码:
template <typename T, typename R>
struct is_dereferenceable_exact
{
typedef char yes;
typedef struct { char dummy[2]; } no;
template <typename U, R (U::*)() const>
struct SFINAE;
template <typename U>
static yes test(SFINAE<U, &U::operator*>*);
template <typename U>
static no test(...);
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};
Run Code Online (Sandbox Code Playgroud)
此结构用于检查给定类型是否可解除引用,并且取消引用的结果具有类型 R.我使用省略号的旧方法,而sizeof不是void_t或故意检测成语 - 我想只使用C++ - 03特征(如果可能的话).
但我只是不能写出类似的结构,它只能确定是否存在T::operator*忽略它的类型.你能帮助我吗?
UPD我可以概括一下我的问题:如果我可以使用返回类型编写一些特征,我可以像上面的引子一样消除它们吗?
也许你也可以使用sizeof操作技巧:
#include <iostream>
template <typename T>
struct is_dereferenceable_exact
{
typedef char yes;
typedef struct { char dummy[2]; } no;
template <int>
struct SFINAE;
template <typename U>
static yes test(SFINAE<sizeof(&U::operator*)>*);
template <typename U>
static no test(...);
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};
struct A {
int operator*() { return 0; }
};
struct B { };
int main() {
std::cout << is_dereferenceable_exact<A>::value << std::endl;
std::cout << is_dereferenceable_exact<B>::value << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |