![](/img/trans.png)
[英]C++ - Why is this candidate template with a function pointer parameter ignored?
[英]C++ working with templates for integral types, candidate template is ignored
我有一個帶有重載operator +
的類operator +
用於添加。 我希望用它來添加一個整數成員的整數類型。 但是,候選模板被忽略。 這樣做的正確方法是什么?
#include <iostream>
using namespace std;
template <class T>
class A
{
public:
A(uint32_t a = 0)
: _a(a)
{ }
template <class TT, typename std::enable_if<std::is_integral<TT>::value>::type>
TT operator + (TT right) { return _a + right; }
private:
uint32_t _a;
};
class AT : public A<AT>
{
public:
AT() : A(10) { }
};
int main()
{
AT at;
cout<< (at + 10);
return 0;
}
第二個模板參數typename std::enable_if<std::is_integral<TT>::value>::type
聲明了一個匿名模板參數,並且無法在調用at + 10
推斷出它。
我想你想要的可能是什么
// specify default value for the 2nd template parameter
template <class TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
T operator + (TT right) { return _a + right; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.