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
andfv_const
不是相同的类型。
是否可以从中派生类型fvc
以fv
使其等于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");