shared_ptr
今後のプロジェクトでかなりの量を使用するつもりなので、( を認識していない) の代用としてstd::make_shared
可変引数テンプレート関数を書きたいと思いました。コンストラクターに. 以下の最小限の例をコンパイルしようとすると、GCC 4.5.2 から次のようになります。spnew<T>(...)
shared_ptr
new
initializer_list
関数 'int main(int, char**)' 内: 関数 'std::shared_ptr spnew(Args ...) [with T = Example, Args = {}]' への引数が多すぎます 関数 'std::shared_ptr spnew(Args ...) [with T = Example, Args = {}]': 'Example::Example()' の呼び出しに一致する関数がありません
奇妙なことに、 を代入すると同等のエラーが発生std::make_shared
しspnew
ます。どちらの場合も、 an が関係しているときにパラメーターを誤って推測しているようで、誤って空としてinitializer_list
扱っています。Args...
次に例を示します。
#include <memory>
#include <string>
#include <vector>
struct Example {
// This constructor plays nice.
Example(const char* t, const char* c) :
title(t), contents(1, c) {}
// This one does not.
Example(const char* t, std::initializer_list<const char*> c) :
title(t), contents(c.begin(), c.end()) {}
std::string title;
std::vector<std::string> contents;
};
// This ought to be trivial.
template<class T, class... Args>
std::shared_ptr<T> spnew(Args... args) {
return std::shared_ptr<T>(new T(args...));
}
// And here are the test cases, which don't interfere with one another.
int main(int argc, char** argv) {
auto succeeds = spnew<Example>("foo", "bar");
auto fails = spnew<Example>("foo", {"bar"});
}
これは単なる私の見落としですか、それともバグですか?