私は以下のクラスがあります:私は働くために、次のコードをしたいワイド様々な
class Foo
{
public:
// Constructors here
private:
std::vector<X> m_data; // X can be any (unsigned) integer type
};
を:
Foo f0;
Foo f1(1); // and/or f1({1})
Foo f2(1, 2); // and/or f2({1, 2})
Foo f3(1, 2, 3); // and/or f3({1, 2, 3})
Foo f4(1, 2, 3, 4); // ... and so on
std::vector<int> vec = {1, 2, 3, 4};
Foo f5(vec);
Foo f6(vec.begin(), vec.end());
std::list<std::size_t> list = {1, 2, 3, 4};
Foo f7(list);
Foo f8(list.begin(), list.end());
std::any_iterable_container container = {1, 2, 3, 4};
Foo f9(container);
Foo f10(container.begin(), container.end());
// PS: I guess I only want containers/iterators that store individual
// values and not pairs (e.g., I don't want care about std::map
// because it does not make sense anyway).
これまでのところ、私はSFINAEを結合しようとしましたが、コンストラクタすべての可能な型、可変的なテンプレートなどでオーバーロードします。私が1つのコンストラクタのケースを修正するたびに、別のものが故障します。また、私が書いたコードは非常に複雑で読みにくいものになっています。しかし、問題はかなりシンプルなようで、間違った方法で近づいていると思います。コードをできるだけシンプルに保ちながら、コンストラクタを書く方法(理想的にはC++ 17)の提案は歓迎する以上のものです。
ありがとうございます。
Foo()
がデフォルト(ノンパラメトリック)コンストラクタです:
template <typename X>
class Foo
{
public:
Foo() { };
Foo(initializer_list<int> l) :m_data(l) { };
template<typename container>
Foo(container const & c) :m_data(c.begin(), c.end()) {};
template<typename iterator>
Foo(iterator begin, iterator end) :m_data(begin, end) { };
private:
std::vector<X> m_data;
};
:
私はこの問題は、あなたが信じているほど単純であることを確認していませんそうです。 –
あなたが試したこととそのエラーを表示できますか? –