简体   繁体   English

C ++:在main.cpp中声明结构的任何可能性,但在头文件中具有类的模板

[英]C++ : Any possibility to declare a struct in main.cpp but having a template with class in header

As a beginner in C++ with templates I have a question, sorry if it's ridiculous. 作为使用模板的C ++的初学者,我有一个问题,对不起,这很可笑。

With having a template in header file like this one: 在头文件中有这样的模板:

template <class T> class MyClass

is it possible to define a structure in the main.cpp like this: 是否可以在main.cpp中定义如下结构:

struct CC;

struct CC
{

     MyClass (CC) p;
     CC() : p(0){}
}; 

Or : 要么 :

struct Foo {

    MyClass<struct Foo> bar;
    MyClass<std::string> text;
};

I am very lost at this. 我对此很迷失。 Sorry. 抱歉。

First, whether the template is in a header file or not, does not matter. 首先,模板是否在头文件中都没有关系。 The compiler proper only sees the code after preprocessing, and at that point there are no files. 适当的编译器仅在预处理后才能看到代码,此时没有文件。 Just text. 只是文字。

Let's consider a variation of your first example: 让我们考虑您的第一个示例的变体:

template< class T >
class MyClass
{
    int x;
};

struct CC
{

     MyClass<CC> m;
     CC() {}
}; 

auto main() -> int {}

This compiles fine because the MyClass template doesn't actually use CC for anything, and so no knowledge of it other than that it's a type, is needed. 这样编译就可以了,因为MyClass模板实际上不使用CC做任何事情,因此除了它是类型之外,不需要其他知识。

But let's say that its size is used. 但是,可以说使用了它的大小。 That requires a complete type . 那需要一个完整的类型 Or put another way, a complete type is a type whose size is known. 或者换句话说,一个完整的类型一个类型,其大小是已知的。

template< class T >
class MyClass
{
    char x[sizeof( T )];
};

struct CC
{

     MyClass<CC> m;
     CC() {}
}; 

auto main() -> int {}

This shall not compile, because at the point where MyClass<CC> is used, the size of CC is not yet known. 这不应编译,因为在其中点MyClass<CC>被使用,尺寸CC还不知道。 Additional data members, or eg a virtual function, could be defined later in the class definition. 稍后可以在类定义中定义其他数据成员,例如虚拟函数。 That would increase the size. 那会增加大小。


As a near counter-example, consider this third variant: 作为一个近乎反例的示例,请考虑以下第三个变体:

template< class T >
class MyClass
{
public:
    void foo()
    {
        char x[sizeof( T )];
    }
};

struct CC
{

     MyClass<CC> m;
     void use_foo() { m.foo(); }
     CC() {}
}; 

auto main() -> int {}

This compiles fine, but why? 这样编译可以,但是为什么呢? Apparently also here the template requires a complete CC type? 显然这里的模板也需要完整的CC类型?

But no, the compiler treats member function definitions in a class as if they'd been declared inline and defined outside of the class, like this: 但是不,编译器将类中的成员函数定义视为inline声明并在类外部进行定义,如下所示:

template< class T >
class MyClass
{
public:
    inline void foo();
};

template< class T >
void MyClass<T>::foo()
{
    char x[sizeof( T )];
}

struct CC
{

     MyClass<CC> m;
     inline void use_foo();
     CC() {}
}; 

void CC::use_foo() { m.foo(); }

auto main() -> int {}

And from this you can see that the MyClass template itself does not depend on knowledge of the size of CC . 从中您可以看到MyClass模板本身不依赖于CC大小的知识。

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

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