Const函数签名来自函数签名
const function signature from function signature
typedef void fv();
typedef std::add_const<fv>::type fvc;
typedef void fv_const() const;
static_assert(std::is_same<fvc,fv_const>::value,"Oops");
这不能编译,因为fvc
和fv_const
不是相同的类型。
是否可能由fv
导出类型fvc
,使其等于fv_const
?
将const
添加到非成员函数签名中没有多大意义,但是您可以这样做:
template<class T> struct add_f_const;
template<class R, class...Args>
struct add_f_const<R (Args...)> { typedef R type(Args...) const; };
static_assert(std::is_same<add_f_const<void()>::type, void() const>::value, "Oops");
通常将const
添加到成员函数中:
template<class T> struct add_member_const;
template<class T, class R, class...Args>
struct add_member_const<R (T::*)(Args...)> { using type = R (T::*)(Args...) const; };
struct test {};
static_assert(std::is_same<add_member_const<void (test::*)()>::type,
void (test::*)() const>::value, "Oops");
相关文章:
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 从函数返回const char*数组
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 在 const 函数中通过引用和指针返回之间的区别
- 使用 std::string () const 函数启动线程或未来
- 在运算符重载定义中使用成员函数(const错误)
- 使用共享指针的函数调用,其对象应为 const
- 将函数参数"const char*"转换为"std::string_view"是
- 为什么返回类型的'const'限定符对标有 __forceinline/内联的函数没有影响?
- 使用按引用调用时,不能在没有对象的情况下调用成员函数 const
- 在呼叫运算符函数const中调用运算符时错误
- 我需要用c ++ 98重写c ++ 11代码,c ++ 11正在使用lambda函数[&](const Output & o
- 函数 const 本地对象在C++会发生什么
- int const函数(参数)、int函数(const参数)和int函数(parameters)const之间的区别是什
- const_cast为函数const设置规则并破坏规则
- 构造函数"const variables"设置的用于表示C++数组的边界?
- 将char**传递给C++中的函数(const char**)
- 复制构造函数const char*和Shared_ptr
- 使函数"const"的意图是什么
- c++嵌套映射不匹配成员函数const成员