类模板Example< int&gt ;;语句对于C ++ 11意味着什么? [英] What does `class template Example<int>;` statement mean with C++11?

查看:80
本文介绍了类模板Example< int&gt ;;语句对于C ++ 11意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在cplusplus.com上,我曾被引用显式模板实例化 ,给出以下示例:

I've been referred to "Explicit Template Instantiation" at cplusplus.com, which gives the following example:

template <typename T> class Example
{
public:
    Example( T test )
    {
        _data = test;
    }
    void setTest(T test)
    {
        _data = T;
    }
private:
    T _data;
};

class template Example<int>;
class template Example<float>;
class template Example<double>;

除了对我来说似乎遗漏的错误(尝试将类型分配给成员)变量- _data = T 而不是我假设的应该是 _data = test -我不明白

Apart from what looks like an omission error to me where a type is attempted to be assigned to a member variable -- _data = T instead of what I assume should be _data = test -- what I don't understand is what do the last 3 lines declare or instruct the compiler to do, exactly?

我知道最后的三行是什么,或者确切地指示编译器做什么?实例化和专业化。我对后两者的理解可能确实有一些漏洞,但是我通常使用例如 template class Example< int> ;; 表单,而不是摘要中显示的表单。

I know what templates are, have built programs with them, and know in general about their instantiation and specialization. I do probably have some holes in the understanding of the latter two, but I typically instruct an explicit template instantiation using e.g. template class Example<int>; form and not the one shown in the snippet.

我试图使用 g ++ -std = c ++ 11 -pedantic 编译代码段,它编译得很好并且没有警告(我纠正了 _date = T 上面的错误)。

I've tried to compile the snippet using g++ -std=c++11 -pedantic and it compiles just fine and without warnings (I corrected the _date = T error above first).

这是在我评论有关问题的答案,但我仍不确定代码段中的后3行是模板特化还是实例化。

This came after I commented on an answer to a related question and I am still unsure whether either of the last 3 lines in the snippet is a template specialization or instantiation.

我有还尝试在类之后允许模板的语法) ://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf rel = nofollow noreferrer>由ISO发布的C ++ 11草案,但是空手而归。

I have also tried to locate the relevant grammar production rule (one allowing template after class) in the C++11 draft published by ISO but came empty handed.

推荐答案

我们可以从以下Godbolt中看到示例,根据clang和MSVC格式不正确,请查看显式实例化部分 [temp.explicit] 我看不到gcc接受它的任何理由。

We can see from the following godbolt example this is ill-formed according to clang and MSVC and looking at the draft standard section on Explicit instantiation section [temp.explicit] I don't see any justification for gcc to accepts it.

我认为给定该主题的文章可能的意思是:

I believe what the article "possibly" meant given the topic was:

template class Example<int>;
template class Example<float>;
template class Example<double>;

与gcc / clang / MSVC格式良好

在C ++ 11之前的版本中,似乎允许这种语法,请参见缺陷报告1707:精化类型说明符中的模板没有嵌套名称说明符强调我的):

It looks like pre C++11 this grammar was allowed, see defect report 1707: template in elaborated-type-specifier without nested-name-specifier (emphasis mine):


精心制作的类型语法10.1.7.3 [dcl.type.elab]中的说明符
部分读取

The grammar for elaborated-type-specifier in 10.1.7.3 [dcl.type.elab] reads, in part,


elaborated-type-specifier:
    class-key nested-name-specifieropt templateopt simple-template-id


允许使用不带
嵌套名称说明符的模板关键字,例如结构模板S。
b与template关键字的其他用法不一致。将生产一分为二可能更好
,并且只允许在嵌套名称说明后面加上关键字

This allows use of the template keyword without a nested-name-specifier, e.g., struct template S. This is inconsistent with other uses of the template keyword. It might be better to split the production in two and only allow the keyword following a nested-name-specifier,

...。

所以这更有意义带有此评论,表示 -ansi 会引起警告。

So this makes a little more sense with this comment that -ansi causes a warning.

另一个 answerer提交了两个错误报告

cppreference 具有很好的显式实例化这样的问题显式实例化-什么时候使用?解释了更多细节,为什么这很有用。

cppreference has a good dicssuion of Explicit instantiation and this SO question Explicit instantiation - when is it used? explains more details why this is useful.

另外请注意,我们可以看到此元发布:L墨水已更改为cppreference.com ,表明该网站的信息不正确,并且一般而言,社区更喜欢 cppreference 作为可靠的C ++参考。

Also note, we can see this Meta post: Links being changed to cppreference.com that the site has been known to have incorrect information and in general the community prefers cppreference as a solid C++ reference.

这篇关于类模板Example&lt; int&gt ;;语句对于C ++ 11意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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