問題描述
在 cppreference 的 std::enable_if
的語(yǔ)言參考中 包含以下注釋
In the language reference of std::enable_if
at cppreference the following note is included
注意事項(xiàng)
一個(gè)常見的錯(cuò)誤是聲明兩個(gè)不同的函數(shù)模板僅在它們的默認(rèn)模板參數(shù)中. 這是非法的,因?yàn)槟J(rèn)模板參數(shù)不是函數(shù)模板的一部分簽名,并聲明兩個(gè)不同的函數(shù)模板相同的簽名是非法的.
A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different function templates with the same signature is illegal.
在下面示例中的模板函數(shù)中,在我看來,這種情況發(fā)生了.即,兩個(gè)模板函數(shù) onlyForDerivedObjects(...)
似乎(對(duì)我而言)僅在默認(rèn)模板參數(shù)上有所不同.我意識(shí)到我在這里遺漏了一些東西,希望有人可以向我解釋這一點(diǎn),或者為我指明方向,我可以找到自己的頓悟.
In the template functions in example below, it seems to me that this situation occurs. I.e., the two template functions onlyForDerivedObjects(...)
seem (to me) to differ only by their default template arguments. I realize I am missing something here, and hopefully someone can explain this to me, or point me in the direction to where I might find an epiphany for myself.
- 問題: W.r.t.上面的引用,為什么下面的例子編譯和運(yùn)行良好:當(dāng)我認(rèn)為它產(chǎn)生具有兩個(gè)模板的情況時(shí),我是否錯(cuò)誤分類了下面模板函數(shù)中的
typename std::enable_if ...
部分僅在默認(rèn)模板參數(shù)方面不同的函數(shù)?
- Question: W.r.t. the quote above, why do the example below compile and run fine: do I misclassify the
typename std::enable_if ...
part in the template functions below when I consider it to yield a situation with two template functions which differ only in their default template argument?
- 現(xiàn)場(chǎng)演示
基類和派生類:
class BaseA
{
public:
int getInt() const { return 21; };
};
class DerivedA : public BaseA {};
class BaseB
{
public:
int getAnotherInt() const { return 33; };
};
class DerivedB : public BaseB {};
具有以下模板函數(shù)
/* template functions that, seemingly, only differ in their
default template arguments? */
template< class T,
typename std::enable_if<std::is_base_of<BaseA, T>::value>::type* = nullptr >
int onlyForDerivedObjects(const T& obj)
{
return 2*obj.getInt();
}
template< class T,
typename std::enable_if<std::is_base_of<BaseB, T>::value>::type* = nullptr >
int onlyForDerivedObjects(const T& obj)
{
return 3*obj.getAnotherInt();
}
編譯并運(yùn)行良好(g++ -Wall -std=c++11 ...
, g++ 4.9.3
)
compiles and runs fine (g++ -Wall -std=c++11 ...
, g++ 4.9.3
)
#include <iostream>
#include <type_traits>
/* ... classes and template functions as above */
/* template argument deduction seems to work fine */
int main()
{
DerivedA* objA = new DerivedA();
DerivedB* objB = new DerivedB();
std::cout << onlyForDerivedObjects(*objA) << std::endl; // 42
std::cout << onlyForDerivedObjects(*objB) << std::endl; // 99
return 0;
}
推薦答案
注意事項(xiàng)
一個(gè)常見的錯(cuò)誤是聲明兩個(gè)僅在默認(rèn)模板參數(shù)上不同的函數(shù)模板.這是非法的,因?yàn)槟J(rèn)模板參數(shù)不是函數(shù)模板簽名的一部分,并且聲明具有相同簽名的兩個(gè)不同函數(shù)模板是非法的.
A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different function templates with the same signature is illegal.
您的函數(shù)不僅在默認(rèn)模板參數(shù)方面有所不同,而且在模板參數(shù)方面也有所不同,因此具有不同的簽名.
Your functions don't differ only in their default template arguments, they differ in their template parameters, so have different signatures.
在這兩種情況下,默認(rèn)模板參數(shù)都是nullptr
,但第二個(gè)模板參數(shù)在每種情況下都不同.
In both cases the default template argument is nullptr
, but the second template parameter is different in each case.
這篇關(guān)于使用 std::enable_if 作為模板時(shí)的默認(rèn)模板參數(shù).參數(shù):為什么兩個(gè)模板函數(shù)只在 enable_if 參數(shù)上不同?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!