1
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");

这不会编译,因为fvcandfv_const不是相同的类型。

是否可以从中派生类型fvcfv使其等于fv_const

4

1 回答 1

0

添加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");
于 2013-03-14T15:06:28.343 回答