[英]c++ template class pass constructor by ref depending on type
假設我有一個看起來像這樣的類,如果T
是一個像double
這樣的簡單類型,或者如果T
更復雜,那么它應該由值構造。
到目前為止我的代碼看起來像這樣:
template<class T>
class Val {
public:
Val() = default;
Val(double v) : _v(v) {}
template<typename U = T>
Val(const &T v,
typename std::enable_if<!std::is_same<U,double>::value, bool>::type = 0)
: _v(v) {}
private:
T _v;
};
哪個有效,但感覺非常粗略,因為在構造函數中引入了一個額外的參數。 這個問題有更好的解決方案嗎? 這似乎更適合過載或模板專業化解決方案? 對於所有簡單類型( int
, float
, double
......),這通常可以解決嗎?
您只需要一個構造函數。 畢竟,它在兩種情況下做同樣的事情對嗎? 首先,定義一個基於T
的類型特征,它是值或引用:
template <typename T>
using param_type = std::conditional_t<
is_complex_type<T>::value,
T const&,
T>;
其中is_complex_type
是稍后要確定的某種適當的類型特征。 也許這是is_fundamental
其他答案建議。
然后使用它:
template<class T>
class Val {
public:
Val() = default;
Val(param_type<T> v) : _v(v) { }
};
有std::is_fundamental
應該適合你。 對我來說最好看的解決方案是:
template<class T>
typename std::enable_if< std::is_fundamental<T>::value >::type func(T p_arg){}
template<class T>
typename std::enable_if< !std::is_fundamental<T>::value >::type func(T const &p_arg){}
只需對代碼進行一些修改后的版本以及CyberGuy對std::is_fundamental
的推薦std::is_fundamental
。
#include <iostream>
using namespace std;
template <class T>
class Val
{
public:
template <typename U = T>
Val(T v,
typename std::enable_if<std::is_fundamental<U>::value>::type* = 0)
: _v(v)
{
cout << "fundamental" << endl;
}
template <typename U = T>
Val(T const& v,
typename std::enable_if<!std::is_fundamental<U>::value>::type* = 0)
: _v(v)
{
cout << "complex" << endl;
}
private:
T _v;
};
struct A {};
int main()
{
Val<double> v1(1);
Val<char> v2('a');
Val<A> v3(A{});
}
輸出:
fundamental
fundamental
complex
您可以使用boost::call_traits<T>::param_type
template<class T>
class Val {
public:
Val() = default;
Val(boost::call_traits<T>::param_type v) : _v(v) {}
private:
T _v;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.