typedef 具有所有默认参数的模板

发布于 2024-11-15 03:25:36 字数 358 浏览 2 评论 0原文

我声明一个模板类,其中所有参数都具有默认参数,例如:

template<typename TYPE = int>
class Foo {};

那么以下两个是等效的:

Foo<int> one;
Foo<> two;

但是,我不允许这样做:

Foo three;

是否可以使用 typedef 来实现这一点名称相同但没有括号,如下所示:

typedef Foo<> Foo;

I declare a templated class with all parameters having default arguments, for example:

template<typename TYPE = int>
class Foo {};

Then the following two are equivalent:

Foo<int> one;
Foo<> two;

However, I'm not allowed to just do:

Foo three;

Is it possible to achieve that with a typedef to the same name but without the brackets, like this:

typedef Foo<> Foo;

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

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

发布评论

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

评论(6

顾挽 2024-11-22 03:25:36

我做了如下的事情,不知道你是否喜欢:

template<typename TYPE = int>
class basic_Foo {};

typedef basic_Foo<int> Foo;

I do something like the following, I don't know if you will like it or not:

template<typename TYPE = int>
class basic_Foo {};

typedef basic_Foo<int> Foo;
夏夜暖风 2024-11-22 03:25:36

你不能用不同的类型重新声明一个符号,所以无论你能做什么都不会像你期望的那样工作。
如果您想实现此目的,请使用不同的名称作为别名:

typedef Foo<> Foo_;

You can't redeclare a symbol with a different type, so whatever you will be able to do won't work as you expect.
If you want to achieve this, use a different name as alias :

typedef Foo<> Foo_;
坦然微笑 2024-11-22 03:25:36

如果声明 typedef Foo<> Foo; 是允许的,其后是名称
无法将 Foo 指定为模板。
即,以下内容无效。

template< template< class > class > struct A {...};
A< Foo > a; // error

虽然上面的 typedef 在实践中是不允许的,
如果您仍然需要将 Foo 编写为 Foo<>,如下所示的宏
就会达到目的。

#define Foo Foo<>

If the declaration typedef Foo<> Foo; is allowed, thereafter the name
Foo as a template cannot be specified.
That is, the following becomes invalid.

template< template< class > class > struct A {...};
A< Foo > a; // error

Though the above typedef isn't allowed in practice,
if you still need to write Foo as Foo<>, a macro like the following
will meet the purpose.

#define Foo Foo<>
不可一世的女人 2024-11-22 03:25:36
typedef Foo<> Foo;

给出:

prog.cpp:4: error: ‘typedef class Foo<int> Foo’ redeclared as different kind of symbol
prog.cpp:2: error: previous declaration of ‘template<class TYPE> class Foo’

该错误几乎说明了问题所在。编译器将 Foo 视为被重新声明。

但是,这应该编译并工作:

template<typename TYPE = int> class Foo {};

typedef Foo<> FooClone;

int main()
{
   Foo<int> one;
   Foo<> two;
   FooClone three;

   return 0;
}
typedef Foo<> Foo;

Gives:

prog.cpp:4: error: ‘typedef class Foo<int> Foo’ redeclared as different kind of symbol
prog.cpp:2: error: previous declaration of ‘template<class TYPE> class Foo’

The error pretty much tells what the problem is. Compiler sees Foo as being re-declared.

However, this shall compile and work:

template<typename TYPE = int> class Foo {};

typedef Foo<> FooClone;

int main()
{
   Foo<int> one;
   Foo<> two;
   FooClone three;

   return 0;
}
自找没趣 2024-11-22 03:25:36

不可以。尽管您可以为 class 声明与 class 同名的 typedef,因为您可以使用 typedef 重新定义名称引用它已经引用的类型。

typedef class A A;

或者如果 A 已声明为类:

typedef A A;

您不能使用模板的名称来执行此操作(模板的名称不是类的名称),您必须给它一个不同的名称。

typedef Foo<> Bar;

No. Although you can declare a typedef for a class with the same name as a class because you can use a typedef to redefine a name to refer to the type to which it already refers.

typedef class A A;

or if A was already declared as a class:

typedef A A;

You can't do that with the name of a template (the name of a template isn't a name of a class), you'd have to give it a different name.

typedef Foo<> Bar;
葬シ愛 2024-11-22 03:25:36

不幸的是,不能,因为 Foo 已经是类模板本身的名称,因此不能是同一命名空间中的任何其他名称。

Unfortunately, no, because Foo is already the name for the class template itself, and thus can't be anything else in the same namespace.

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