“使用 typedef-name ... 作为类”关于前瞻性声明

发布于 2024-08-21 11:40:19 字数 362 浏览 11 评论 0原文

我在这里做一些基于策略的设计,我需要输入大量模板类型来缩短名称。
现在的问题是,当我需要使用指向其中一种类型的指针时,我尝试前向声明它,但编译器会抱怨 test.cpp:8: error: using typedef-name 'Test1' after '班级'
这与大小无关,因为我根本不需要 obj,它只是“.h”文件中的一个指针,我不想将整个模板带入其中。
这是 g++:

//Works
class Test{};
class Test;

//Doesn't work
class Test{};
typedef Test Test1;
class Test1;

有什么提示吗?

I'm doing some policy-based designs here and I have the need to typedef lots of template types to shorten the names.
Now the problem comes that when I need to use a pointer to one of those types I try to just forward-declare it but the compiler complains with a test.cpp:8: error: using typedef-name ‘Test1’ after ‘class’

It's nothing to do with sizes as I don't need the obj at all, its just a pointer in a ".h" file where I don't want to bring the whole template in.
This is g++:

//Works
class Test{};
class Test;

//Doesn't work
class Test{};
typedef Test Test1;
class Test1;

Any hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情出卖 2024-08-28 11:40:19

这是正确的。 typedef-name 不能在此类前向声明​​中使用(这在技术上称为详细类型说明符,如果此类说明符解析为 typedef-name,则程序格式错误)。

我不明白为什么您实际上需要首先进行前向声明。因为如果您已经有了 typedef,为什么不直接使用该 typedef 呢?它也表示该类。

编辑:从您的评论来看,您似乎需要一个指定的前向声明标头,这与一样徒劳。因此,如果您的模板名为 Test_Template_Name_Long,则该标头可能类似于

TestFwd.h

template<typename A, typename B> 
class Test_Template_Name_Long;

typedef Test_Template_Name_Long<int, bool> Test1;

然后您可以只使用该标头,而不是执行以下操作class Test1,编译器不知道它是什么(并且会认为它是一个新类,独立于任何模板和 typedef)。

That's correct. A typedef-name cannot be used in such a forward declaration (this is technically called an elaborated type specifier, and if such a specifier resolves to a typedef-name, the program is ill-formed).

I don't understand why you actually need the forward declaration in the first place. Because if you already have the typedef in place, why not just use that typedef? It's just aswell denoting that class.

Edit: From your comment, it seems you need a designated forward declaration header, much in the same vain of <iosfwd>. So, if you template is called Test_Template_Name_Long, this a header can look like

TestFwd.h

template<typename A, typename B> 
class Test_Template_Name_Long;

typedef Test_Template_Name_Long<int, bool> Test1;

Then you can just use that header, instead of doing class Test1, which the compiler has no clue about what it is (and will think it is a new class, independent of any template and typedef).

只有一腔孤勇 2024-08-28 11:40:19
typedef Test Test1;
class Test1;

失败,因为 typedef 语句充当 Test1 类型的声明。

基本上,当编译器看到 typedef 时,它会记住 Test1 是 Test 的同义词。然后,当它看到 class Test1; 时,它认为您正在声明一个新类型。但它不能将其视为声明,因为名称 Test1 已被 typedef 使用。

例如,如果要对 Test1 使用前向声明,则必须使用该 typedef 作为前向声明将 typedef 放入每个文件中。因此,您可以执行以下操作,而不是使用 class Test1;

class Test;
typedef Test Test1;
typedef Test Test1;
class Test1;

fails because the typedef statement serves as the declaration for the Test1 type.

Basically, when the compiler sees the typedef, it remembers that Test1 is a synonym for Test. Then when it sees class Test1;, it thinks there's a new type you are declaring. But it can't treat that as a declaration because the name Test1 is already being used by the typedef.

If you want to use a forward declaration for Test1, for example, you have to put the typedef in each file using that typedef as a forward declaration. Therefore, instead of class Test1;, you'd do:

class Test;
typedef Test Test1;
伪装你 2024-08-28 11:40:19

出现这种情况的另一种情况是:

// File: network_fwd.hpp
#ifndef __NETWORK_FWD_HPP__
#define __NETWORK_FWD_HPP__
class Network;
typedef Network GarnetNetwork;
#endif // __NETWORK_FWD_HPP__

// File: network.hpp
#include "network_fwd.hpp"

class GarnetIntLink : public BasicLink
{
  public:

    friend class GarnetNetwork;

};

我必须这样做,因为实际上有一个名为 GarnetNetwork 的类,我将其合并到 Network 类中,并且我想使用该 typedef 将其传播到代码的其余部分。
GCC 仍然给出以下消息:错误:在“class”之后使用 typedef-name 'GarnetNetwork'

我非常感谢你对这个案子的帮助。

注意:顺便说一句,我在这里发布这个问题是因为我认为这几乎是同一件事,并且为了不分散社区的注意力。

One more case in which this situation arises is this:

// File: network_fwd.hpp
#ifndef __NETWORK_FWD_HPP__
#define __NETWORK_FWD_HPP__
class Network;
typedef Network GarnetNetwork;
#endif // __NETWORK_FWD_HPP__

// File: network.hpp
#include "network_fwd.hpp"

class GarnetIntLink : public BasicLink
{
  public:

    friend class GarnetNetwork;

};

I had to do that because there was actually a class called GarnetNetwork that I merged into class Network, and I wanted to propagate that throughout the rest of the code using that typedef.
GCC still gives this message: error: using typedef-name ‘GarnetNetwork’ after ‘class’.

I really appreciate your help about this case.

NB: By the way I posted this question here because I thought it is pretty much the same thing and in order not to distract the community.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文