简体   繁体   English

类模板专门化时的前向声明

[英]forward declaration at class template specialisation

Is the following forward declaration, at the class template specialisation stage, legal C++ code? 在类模板专用化阶段,以下正向声明是合法的C ++代码吗?

template<typename>
struct Basic
{};

template<>
struct Basic<struct Foo> //<-- Fwd declaration?
{};

struct Foo
{
    Basic<Foo> m_a;
};

int main()
{
    Foo test;
}

It does compile , but I'm not sure if it's legal 确实可以编译 ,但是我不确定它是否合法

template<>
struct Basic<struct Foo>
{};

is legal. 是合法的。

From C++11 Standard : C ++ 11 Standard

A class declaration introduces the class name into the scope where it is declared and hides any class, variable, function, or other declaration of that name in an enclosing scope. 类声明将类名引入声明它的作用域中,并在封闭的作用域中隐藏该类的任何类,变量,函数或其他声明。

Yes, it is legal C++ and it does forward declare struct Foo . 是的,它是合法的C ++,并且确实向前声明了struct Foo

You can also do this in function declarations: 您也可以在函数声明中执行此操作:

void fun(struct foo);

struct foo {};

void fun(struct foo) {}

or with pointers. 或带有指针。

struct foo* pointer;
struct foo {};

Basically anywhere where a complete type is not required. 基本上在不需要完整类型的任何地方。 (Works with class as well.) (也适用于class 。)

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

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