简体   繁体   English

在带有类型后卫的模板文件中使用前向声明

[英]Using forward declaration in a template file with type guards

I'm trying to combine forward declarations in a template library, with type guards to intercept cases of the wrong type of template being used with the wrong type of class. 我正在尝试将模板库中的前向声明与类型防护结合起来,以拦截使用错误类型的类的错误模板类型的情况。 Here is the simplest snippet of code I can get down to, in order to illustrate the problem... 这是我可以理解的最简单的代码片段,目的是说明问题...

Main.cpp Main.cpp的

#include <iostream>
#include "templates.h"

int main()
{
    B<int_array> hello;
    return 0;
}

templates.h templates.h

#include <type_traits>

#define USE_GUARD
#define FORWARD_DECLARE

template<typename T>
using is_pod = typename std::enable_if<std::is_pod<T>::value>::type;

typedef struct {
    int *p; 
    int n;   
} int_array;

template <typename E, typename T >
class A
{

};

#ifdef USE_GUARD
template <typename T, typename = is_pod<typename std::remove_pointer<decltype(T::p)>::type> >
#else
template <typename T>
#endif
class B : public T, public A< B<T>, T >
{                                     // This is the 'curiously recurring template
                                      // pattern' (CRTP)
public:
    //! Default constructor
    B() noexcept
    {
        T::p = nullptr;
        T::n = 0;
    }

#ifdef FORWARD_DECLARE
    void reorder(const int_array & newOrder);
#endif
};

template <typename T> class C : public B<T>
{
public:
    C() noexcept : B<T>()
    {
        T::p = nullptr;
        T::n = 0;
    }
};

#ifdef FORWARD_DECLARE
#ifdef USE_GUARD
template <typename T, typename = is_pod<typename std::remove_pointer<decltype(T::p)>::type> >
#else
template <typename T>
#endif
void B<T>::reorder(const int_array & newOrder)
{
    C<T> myC;
}
#endif

I get error messages as follows: 我收到如下错误消息:

Visual Studio 2015: Visual Studio 2015:

Neither USE_GUARD nor FORWARD_DECLARE defined - works fine 既未定义USE_GUARD也未定义FORWARD_DECLARE-正常工作

Only FORWARD_DECLARE defined - works fine 仅定义FORWARD_DECLARE-可以正常工作

Only USE_GUARD defined - works fine 仅定义了USE_GUARD-可以正常工作

Both defined - C3860: template argument lost following class template name must list parameters in the order used in template parameter list 两者都定义C3860: template argument lost following class template name must list parameters in the order used in template parameter list

g++: 克++:

Neither USE_GUARD nor FORWARD_DECLARE defined - works fine 既未定义USE_GUARD也未定义FORWARD_DECLARE-正常工作

Only FORWARD_DECLARE defined - works fine 仅定义FORWARD_DECLARE-可以正常工作

Only USE_GUARD defined - works fine 仅定义了USE_GUARD-可以正常工作

Both defined - Invalid use of incomplete type 'class B<T> at the point where the function 'reorder' is defined 两者均已定义-在定义函数“重新排序”时Invalid use of incomplete type 'class B<T>

So there's obviously something that I'm doing wrong in the way that I combine the type guard with the forward declaration, but neither compiler is giving me a very intelligible error message. 因此,在将类型保护与正向声明结合使用的过程中,显然存在某些错误,但是没有一个编译器给出了非常清晰的错误消息。 Am I making an elementary error in my syntax, or am I just trying to do something fundamentally silly? 我是在语法中犯了基本错误,还是只是在做一些根本上愚蠢的事情?

Default argument should not be repeated in member definition, and all arguments should be provided: 成员定义中不应重复使用默认参数,而应提供所有参数:

#ifdef FORWARD_DECLARE
# ifdef USE_GUARD
template <typename T, typename U>
void B<T, U>::reorder(const int_array & newOrder)
# else
template <typename T>
void B<T>::reorder(const int_array & newOrder)
# endif
{
    C<T> myC;
}
#endif

Demo 演示

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM