我很困惑std::is_const
在标识const
指针时的行为为非const
。 我自己实现的is_const
完全一样。我不知道为什么更通用的模板结构<T>
正在挑选<const T>
版本。 gcc4.7和clang3.1-svn都显示相同的行为。任何人都可以解释发生了什么?下面的代码给出:std :: is_const将const指针标识为非const
#include <iostream>
#include <sstream>
#include <type_traits>
class CEmptyClass {};
namespace jbc
{
template <typename T>
struct is_const : std::false_type {};
template <typename T>
struct is_const<const T> : std::true_type {};
}
int main(int argc, char* argv[])
{
std::cout << "Is 'const CEmptyClass*' constant according to std lib : "
<< std::is_const<const CEmptyClass*>::value << std::endl;
std::cout << "Is 'const CEmptyClass*' constant according to jbc : "
<< jbc::is_const<const CEmptyClass*>::value << std::endl;
}
在这两种情况下is_const<const CEmptyClass*>::value
返回0
*我对'std :: is_const'在识别const指针时的行为感到困惑 - 非const。*代码中没有* const指针*,只有*非const指向const的指针* –
您'很对。我正在考虑将const指向const,而不是。 – jbcoe