本文翻譯自:Difference of keywords 'typename' and 'class' in templates?
For templates I have seen both declarations: 對於模板,我看到了兩個聲明:
template < typename T >
template < class T >
What's the difference? 有什麼不同?
And what exactly do those keywords mean in the following example (taken from the German Wikipedia article about templates)? 在下面的示例中這些關鍵字到底是什麼意思(摘自關於模板的Wikipedia文章)?
template < template < typename, typename > class Container, typename Type >
class Example
{
Container< Type, std::allocator < Type > > baz;
};
#1樓
參考:https://stackoom.com/question/8UWn/模板中關鍵字-typename-和-class-的區別
#2樓
This piece of snippet is from c++ primer book. 這段代碼來自c ++入門書。 Although I am sure this is wrong. 雖然我確定這是錯誤的。
Each type parameter must be preceded by the keyword class or typename: 每個類型參數必須以關鍵字class或typename開頭:
// error: must precede U with either typename or class
template <typename T, U> T calc(const T&, const U&);
These keywords have the same meaning and can be used interchangeably inside a template parameter list. 這些關鍵字具有相同的含義,可以在模板參數列表中互換使用。 A template parameter list can use both keywords: 模板參數列表可以同時使用兩個關鍵字:
// ok: no distinction between typename and class in a template parameter list
template <typename T, class U> calc (const T&, const U&);
It may seem more intuitive to use the keyword typename rather than class to designate a template type parameter. 使用關鍵字typename而不是使用類來指定模板類型參數似乎更直觀。 After all, we can use built-in (nonclass) types as a template type argument. 畢竟,我們可以將內置(非類)類型用作模板類型參數。 Moreover, typename more clearly indicates that the name that follows is a type name. 此外,typename更清楚地指示後面的名稱是類型名稱。 However, typename was added to C++ after templates were already in widespread use; 但是,在模板已經廣泛使用之後,typename被添加到C ++中。 some programmers continue to use class exclusively 一些程序員繼續專門使用類
#3樓
- No difference 沒有不同
- Template type parameter
Container
is itself a template with two type parameters. 模板類型參數Container
本身就是帶有兩個類型參數的模板。
#4樓
For naming template parameters, typename
and class
are equivalent. 對於命名模板參數, typename
和class
是等效的。 §14.1.2: 第14.1.2條:
There is no semantic difference between class and typename in a template-parameter. 模板參數中的類和類型名之間沒有語義上的區別。
typename
however is possible in another context when using templates - to hint at the compiler that you are referring to a dependent type. 但是,在使用模板的另一個上下文中,可以使用typename
提示編譯器您引用的是依賴類型。 §14.6.2: 第14.6.2條:
A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename. 除非在適用的名稱查找中找到類型名稱或該名稱由關鍵字typename限定,否則假定模板聲明或定義中使用的且依賴於模板參數的名稱不會命名類型。
Example: 例:
typename some_template<T>::some_type
Without typename
the compiler can't tell in general whether you are referring to a type or not. 沒有typename
,編譯器通常不會告訴您是否在引用類型。
#5樓
While there is no technical difference, I have seen the two used to denote slightly different things. 雖然沒有技術上的差異,但我已經看到兩者用來表示略有不同的事物。
For a template that should accept any type as T, including built-ins (such as an array ) 對於應該接受任何類型爲T的模板,包括內置函數(例如array)
template<typename T>
class Foo { ... }
For a template that will only work where T is a real class. 對於僅在T是實類的情況下才有效的模板。
template<class T>
class Foo { ... }
But keep in mind that this is purely a style thing some people use. 但是請記住,這純粹是某些人使用的樣式。 Not mandated by the standard or enforced by compilers 未由標準強制要求或由編譯器強制執行
#6樓
typename
and class
are interchangeable in the basic case of specifying a template: 在指定模板的基本情況下, typename
和class
可互換:
template<class T>
class Foo
{
};
and 和
template<typename T>
class Foo
{
};
are equivalent. 是等效的。
Having said that, there are specific cases where there is a difference between typename
and class
. 話雖如此,在特定情況下, typename
和class
有所不同。
The first one is in the case of dependent types. 第一個是在依賴類型的情況下。 typename
is used to declare when you are referencing a nested type that depends on another template parameter, such as the typedef
in this example: 當引用依賴於另一個模板參數的嵌套類型時, typename
用於聲明,例如本示例中的typedef
:
template<typename param_t>
class Foo
{
typedef typename param_t::baz sub_t;
};
The second one you actually show in your question, though you might not realize it: 您實際上在問題中顯示的第二個,儘管您可能沒有意識到:
template < template < typename, typename > class Container, typename Type >
When specifying a template template , the class
keyword MUST be used as above -- it is not interchangeable with typename
in this case (note: since C++17 both keywords are allowed in this case) . 指定模板模板時 ,必須按上述方式使用class
關鍵字-在這種情況下,它不能與typename
互換(注意:由於C ++ 17,在這種情況下,兩個關鍵字都允許) 。
You also must use class
when explicitly instantiating a template: 在顯式實例化模板時,還必須使用class
:
template class Foo<int>;
I'm sure that there are other cases that I've missed, but the bottom line is: these two keywords are not equivalent, and these are some common cases where you need to use one or the other. 我確定還有其他情況,但是最重要的是:這兩個關鍵字不相同,這是一些您需要使用其中一個的常見情況。