【问题标题】:decltype for member functions成员函数的 decltype
【发布时间】:2012-04-13 17:27:28
【问题描述】:

下例中如何获取成员函数的返回类型?

template <typename Getter>
class MyClass {
   typedef decltype(mygetter.get()) gotten_t;
 ...
};

当然,问题是我在定义 MyClass 时没有“mygetter”对象。

我要做的是:我正在创建一个缓存,它可以使用 getter 返回的任何内容,因为它是关键。

【问题讨论】:

    标签: c++ c++11 decltype


    【解决方案1】:

    我不太确定你想要什么,但似乎mygetter 应该只是Getter 类型的任何对象。使用std::declval获取这样的对象,无需其他任何东西(只能用于类型推导)

    typedef decltype(std::declval<Getter>().get()) gotten_t;
    

    【讨论】:

    • 哦,嘿,我从来不知道。棒极了!我一直使用Getter().get() 并假设Getter 是默认可构造的。
    猜你喜欢
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2019-03-02
    相关资源
    最近更新 更多