正如问题所说,我能否通过typetraits找出类型是否有const修饰符
发布于 2012-09-17 04:31:55
在C++11中,您可以使用std::is_const。只需包含<type_traits>标头。
在C++03中,很容易自己实现这一点:
template<typename T>
struct is_const
{
const static bool value = false;
};
template<typename T>
struct is_const<const T>
{
const static bool value = true;
};发布于 2012-09-17 04:29:50
如果你有c++11支持,你可以使用std::is_const。否则,请使用boost::is_const。
struct Foo {};
#include <iostream>
#include <type_traits>
....
std::cout << std::is_const<Foo>::value << '\n'; // false
std::cout << std::is_const<const Foo>::value << '\n'; // truehttps://stackoverflow.com/questions/12450458
复制相似问题