在模板实例化和外部模板声明中使用typedef [英] using typedef in template instantiation and extern template declaration

查看:55
本文介绍了在模板实例化和外部模板声明中使用typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在两种情况下,当 extern模板声明显式模板实例化时, typedef 使我感到困惑.

There are two cases where typedef confuses me when it comes to extern template declaration and explicit template instantiation.

为说明这两个,请参见下面的2个示例代码段.

To illustrate the two see below 2 example code snippets.

考虑以下示例(案例1):

// suppose following code in some cpp file    
template <typename T>
    struct example
{
    T value;
};

// valid typedefs
typedef example<int> int_example;
typedef example<std::string> string_example;

// explicit instantiation using above typedefs
template class int_example; // -> compile time error
template class string_example; // -> compile time error

// instead we need to use type names
template class example<int>; // -> OK
template class example<std::string>; // -> OK

// QUESTION 1: Why does this work however? is this valid code?
typedef std::string type_string;
template class example<type_string>;

为什么 template class example< type_string> 与typedef一起使用?为什么在模板类string_example 无效的情况下有效?

Why does the template class example<type_string> work with typedef ? and why is it valid while template class string_example is not?

考虑以下示例(案例2):

// suppose following code is in some header file
template <typename T>
struct example
{
    T value;
};

// valid typedefs
typedef std::string type_string;
typedef example<type_string> string_example;

// Explicit instantiation declaration
// QUESTION 2: Is this valid code? if not why not?
extern template string_example; // -> at least this compiles, but is it OK?

正如上面的评论所质疑的,像上面的示例一样,在 extern模板声明中使用typedef是否有效,为什么编译不同于 Case1 不会.

As questioned in the comment above, is it valid to use typedef in extern template declaration, like in the example above, and why does this compile unlike the Case1 where it does not.

我已经读过类似的案例,但是没有一个可以对上述两个问题给出详细的答案.非常感谢详细的阐述!

I've read about similar cases but none gives the detailed answer to above 2 questions. detailed elaboration is very much appreciated!

推荐答案

template class int_example;

不合法.从C ++ 11 Stanard:

is not legal. From the C++11 Stanard:

14.7.2显式实例化

2显式实例化的语法为:

2 The syntax for explicit instantiation is:

显式实例:
外部 opt 模板 声明

有两种形式的显式实例化:显式实例定义和显式实例声明.显式实例化声明以 extern 关键字开头.

There are two forms of explicit instantiation: an explicit instantiation definition and an explicit instantiation declaration. An explicit instantiation declaration begins with the extern keyword.

3如果显式实例化是针对某个类或成员类的,则声明中的 elaborated-type-specifier 应包含一个 simple-template-id

3 If the explicit instantiation is for a class or member class, the elaborated-type-specifier in the declaration shall include a simple-template-id.

simple-template-id A.12模板部分中定义为:

简单模板ID:
template-name < template-argument-list opt > >

simple-template-id:
template-name < template-argument-listopt >

int_example 不符合 simple-template-id 的要求.
example< int> 确实具有 simple-template-id 的资格.

int_example does not qualify as a simple-template-id.
example<int> does qualify as a simple-template-id.

但是,按照这种逻辑,

extern template string_example;

也不合法.我不知道它如何为您工作.当我尝试在g ++ 4.9.3中编译这样的行时,出现以下错误.

is not legal either. I don't know how it works for you. I got the following error when I tried to compile such a line in g++ 4.9.3.

socc.cc:15:31: error: expected unqualified-id before ‘;’ token
 extern template string_example; // -> compile time error

这篇关于在模板实例化和外部模板声明中使用typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆