bad*_*ash 4 c++ templates specialization
我正在阅读http://bartoszmilewski.wordpress.com/2009/10/21/what-does-haskell-have-to-do-with-c/并遇到此代码以检查类型是否为指针:
template<class T> struct
isPtr {
static const bool value = false;
};
template<class U> struct
isPtr<U*> {
static const bool value = true;
};
template<class U> struct
isPtr<U * const> {
static const bool value = true;
};
Run Code Online (Sandbox Code Playgroud)
我如何专门化通用模板来处理const指针到const类型的情况?如果我这样做:
std::cout << isPtr <int const * const>::value << '\n';
Run Code Online (Sandbox Code Playgroud)
当我期待虚假时,我得到一个真实的.谁能解释一下?
编辑:使用VC++ 2010编译器(快递:-)
结果只是正确的; 你打电话时调用你的第三个专业isPtr <int const * const>; 你要设置的true.
在这种情况下,您可以选择enum,bool因为您有3种状态:
enum TYPE
{
NOT_POINTER,
IS_POINTER,
IS_CONST_POINTER
};
template<class T> struct
isPtr {
static const TYPE value = NOT_POINTER;
};
template<class U> struct
isPtr<U*> {
static const TYPE value = IS_POINTER;
};
template<class U> struct
isPtr<U * const> {
static const TYPE value = IS_CONST_POINTER;
};
Run Code Online (Sandbox Code Playgroud)
这是演示.
| 归档时间: |
|
| 查看次数: |
1527 次 |
| 最近记录: |