简体   繁体   English

使用派生的模板参数类型作为函数的返回类型(CRTP)?

[英]Use the template parameter type of derived as return type for function (CRTP)?

I've recreated the problem I'm having in the code below: 我已经重新创建了以下代码中的问题:

template<typename T>
class A{
    using type = T::type;
    type someFunction(/*some parameters*/){/*code for the function*/}
    //other stuff
};

template<typename T>
class B : public A<B<T>>{
    typedef T type;
    //other stuff
};

The problem is that I need to have A have a function with a return type of T::type , but because B isn't completely declared at the time when A is compiled, I get the error invalid use of incomplete type 'class B<int>' when I try to compile it (where int could be replaced with any other type). 问题是我需要让A具有返回类型为T::type的函数,但是由于在A编译时B并未完全声明B,所以我会收到错误消息,错误地invalid use of incomplete type 'class B<int>'当我尝试对其进行编译时(可以将int替换为任何其他类型)。 Is there any way to get this to work? 有什么办法可以使它正常工作吗?

You can achieve it if you move the definition of B<T>::type to an external traits-class: 如果将B<T>::type的定义移至外部特征类,则可以实现:

template <typename T>
struct Traits { /* maybe some default values */ };

template<typename T>
class A{
    using type = typename Traits<T>::type;
    type someFunction(/*some parameters*/){/*code for the function*/}
    //other stuff
};

template<typename T>
class B : public A<B<T>>{
    using type = typename Traits<B<T>>::type;
};

template <typename T>
struct Traits<B<T>> {
    using type = T;
};

Is there any way to get this to work? 有什么办法可以使它正常工作吗?

You can use a traits class to derive the type instead of using: 您可以使用traits类来派生类型,而不是使用:

using type = T::type;

Example: 例:

// Declare TypeSelector
template <typename T> struct TypeSelector;

template <typename T>
class A
{
    using type = typename TypeSelector<T>::type;
    type someFunction(/*some parameters*/){ return type {}; }
};

// Declare B.
template <typename T> class B;

// Define TypeSelector for <B<T>> first before defining B<T>
template <typename T> struct TypeSelector<B<T>>
{
    using type = T;
};

template<typename T>
class B : public A<B<T>>{
    using type = typename TypeSelector<T>::type;
    //other stuff
};

int main()
{
   // Compiles fine.
   B<int> a;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CRTP和返回派生类型实例的函数 - CRTP and functions that return an instance of the derived type 在CRTP类中使用派生类型的成员 - Use of member of derived type in CRTP class CRTP与基类试图获取派生类成员的返回类型:无效使用不完整类型 - CRTP with base class trying to get the return type of a derived class member: invalid use of incomplete type 如何在返回类型功能模板的专业化中使用派生类型? (“无法推断模板参数”) - How to use derived type in specialisation of return-type function template? (“couldn't infer template argument”) 使用模板template参数作为类函数的返回类型 - Use template template parameter as return type of class function 使用 class 模板的派生类型作为模板模板参数 - Use derived type of class template as template template parameter 从具有泛型返回类型的crtp的基类调用派生类中的函数 - Calling function in derived class from base class of crtp with generic return type 当 CRTP 派生类型是模板 class 时,公共 class 成员不可见 - Public class member not visible when CRTP derived type is a template class 带有模板模板参数的CRTP派生类 - CRTP derived class with template template parameter 函数返回类型,具有不同的模板参数 - function return type with a different template parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM