【问题标题】:Alias the return type of a const overloaded function给 const 重载函数的返回类型取别名
【发布时间】:2015-06-17 13:42:37
【问题描述】:

我有以下重载函数:

template<size_t N, typename T>
auto get(const T & _t) -> decltype(std::get<...>(_t)) {
    ...
}

template<size_t N, typename T>
auto get(T & _t) -> decltype(std::get<...>(_t)) {
    ...
}

第一个问题是:

第一个使用std::get(const tuple&lt;_Elements...&gt;&amp; __t) 第二个std::get(tuple&lt;_Elements...&gt;&amp; __t)??

现在我想给我的新函数 get 的返回类型加上别名:

using type = typename decltype(aux::get<I>(data))::type;

这里用的是哪一个? const 与否?我该如何选择?我想给两者都取别名!! data 是非常量

【问题讨论】:

    标签: c++ c++11 constants decltype


    【解决方案1】:

    是的,第一个使用const 重载,第二个使用非const 重载。这是因为_t 在第一种情况下是const 而在第二种情况下不是const

    类型别名中使用哪一个取决于data的类型。是const吗?如果是这样,const 重载是别名。如果不是,则非const 是。

    要获取任何类型的“虚拟值”,您可以使用std::declval。此代码将别名为 const 版本:

    using type = typename decltype(aux::get<I>(std::declval<const YourTypeHere>()))::type;
    

    【讨论】:

    • data 是非常量的,我怎样才能得到 const 类型:` typename decltype(aux::get(...))::type` 当参数是 const , 我不想添加新变量
    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 2022-01-13
    • 2013-07-29
    • 1970-01-01
    相关资源
    最近更新 更多