我想做一個類應該繼承其他類的構造函數,但不從這些類本身繼承。如何使用std :: enable_if有條件地選擇可變參數構造函數?
在我的類的初始化期間,我想使用完美轉發來創建一個類型的對象,其構造函數與給定的參數匹配。
除了沒有參數的默認構造函數之外,不應有任何含糊之處。
這是我的代碼:
#include <string>
using namespace std;
//NOTE: this class is just an example to demonstrate the problem
class String {
public:
//default constructor to prevent ambiguity
String() {}
//construct from wstring
template<typename... Args>
String(enable_if<is_constructible<wstring, Args...>::value, Args>::type&&... args) : ws(forward<Args>(args)...) {}
//construct from string
template<typename... Args>
String(enable_if<is_constructible<string, Args...>::value, Args>::type&&... args) : s(forward<Args>(args)...) {}
private:
string s;
wstring ws;
};
void foo(const String& string) {
}
int main()
{
foo(L"123");
foo("123");
return 0;
}
我試過很多東西,但我不能得到它的工作。
- 在目前的方案
enable_if
無法自動扣除模板ARGS(我認爲) - 由於我使用的構造我不能返回值
- 添加其他默認參數
enable_if
韓元使用enable_if
「T的工作,因爲構造函數是可變參數 - 當我從函數的參數,編譯器會抱怨無效的重載
有沒有一種優雅的方式來解決這個問題?
編輯: 是標準允許不應該在我的課會出現一個隱式轉換。 [編輯示例代碼]
與上述示例一起工作的一種解決方案是定義單個可變參數構造函數,並將參數完美地轉發給條件初始化函數。但是,我想避免這種開銷,因爲成員需要被默認構造,並且這可能在其他情況下不起作用。
(隨意編輯的問題,如果事情可以更清楚)
很好的問題,我沒有答案。 FWIW,*沒有* variadic模板它[作品](http://coliru.stacked-crooked.com/a/c799960a369ccf7e)通過使用默認參數。 –
我問附近的相關問題也在:http://stackoverflow.com/questions/18700072/using-sfinae-to-select-different-method-implementations-in-a-clase-template。一個答案也是使用專業化,而不是:-) – Klaus
正如我在Klaus解決方案的評論中所說:只有**一個**用戶定義的轉換是隱式執行的。無論是從字符串文字到'std :: basic_string'還是從'std :: basic_string'到你的'String'類型,但都不是兩者。 – dyp