类型特征不起作用

The*_* do 1 c++ metaprogramming

这个:

cout << std::is_const<const int*>::value; 
Run Code Online (Sandbox Code Playgroud)

打印错误,我认为它应该打印真实.为什么打印错误?

Pot*_*ter 9

因为const int *是一个指向常量整数变量指针.

std::is_const< int * const >::valuetrue因为类型是一个指向变量整数的常量指针.该const到任何结合它之前,除非它是第一个在类型说明符.我避免把它放在第一位,以避免调用这个"特殊规则".

常量指针通常由C++中的引用表示.

要获取有关指向的类型的信息,请使用std::remove_pointer.

std::is_const< typename std::remove_pointer< const int * >::type >::value
Run Code Online (Sandbox Code Playgroud)

true.

  • @There:在你的例子中,指向*to*的类型是const.指针本身可能会改变. (2认同)