使用CRTP时的尾随返回类型用法

时间:2016-03-01 11:34:20

标签: c++11 metaprogramming crtp trailing-return-type

以下是我在CRTP设置中尝试使用尾随返回类型编写的模型代码。

#include <iostream>
#include <memory>
#include <utility>

using namespace std;

struct t_aspect{
    struct t_param1 {};
};

// Generic Selector
template <typename t_detail>
struct Select;

template <>
struct Select<t_aspect::t_param1> {
    using typeof = t_aspect::t_param1;
};

//Base CRTP class
template<typename dclas>
class CrtpB
{
    public:
        template<typename T1>
        auto func1() -> // What should be here?
        {
            return(static_cast<dclas*>(this)->func1<T1>());
        }
};


//Derived CRTP class
class CrtpD : public CrtpB<CrtpD>
{
    private:
        uint32_t param1 = 0;

    private:
        auto func1(const t_aspect::t_param1&) -> uint32_t
        {
            return(param1);
        }

    public:
        static auto create() -> unique_ptr<CrtpB>
        {
            return(unique_ptr<CrtpD>(new CrtpD));
        }

        template<typename T1>
        auto func1() -> decltype(func1(typename Select<T1>::typeof()))
        {
            return(func1(typename Select<T1>::typeof()));
        }
};


int main()
{
    auto crtp = CrtpD::create();
    auto parm = crtp->func1<t_aspect::t_param1>();
    return 0;
}

我想帮一些解释CrtpB中func1的尾随返回类型。

我尝试过使用

decltype(static_cast<dclas*>(this)->func1<T1>())

但这不起作用。我还尝试根据Inferring return type of templated member functions in CRTP中的解决方案编写辅助函数。

template <typename D, typename T>
struct Helpr {
    typedef decltype(static_cast<D*>(0)->func1<T>()) type;
};

但这也不起作用。

1 个答案:

答案 0 :(得分:1)

实例化基类时,

dclas是不完整的类型。你需要做两件事来完成这项工作:

  • 推迟检查func1<T1>()的类型,直到类型完成
  • 在依赖表达式上使用template关键字,以便正确解析模板定义:

我们可以通过添加一个间接层来实现:

namespace detail {
    template <class T, class Func1Arg>
    struct func1_t {
         using type = decltype(std::declval<T>().template func1<Func1Arg>());
    };
};

然后使用此特征作为尾随返回类型:

template<typename T1>
auto func1() -> typename detail::func1_t<dclas,T1>::type
{
    return(static_cast<dclas*>(this)->template func1<T1>());
}