7

我打算shared_ptr在即将到来的项目中使用很多,所以(不知道std::make_shared)我想编写一个可变参数模板函数spnew<T>(...)作为shared_ptr-returning的替代new。一切都很顺利,直到我尝试使用构造函数包含initializer_list. 当我尝试编译下面的最小示例时,我从 GCC 4.5.2 得到以下信息:

在函数'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. 在任何一种情况下,当涉及到时,它似乎都错误地推断出参数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"});
}

这只是我的疏忽还是错误?

4

2 回答 2

1

你可以这样做——

#include <memory>
#include <string>
#include <iostream>
#include <vector>

struct Example {

    template<class... Args>
    Example(const char* t, Args... tail) : title(t) 
    {
        Build(tail...);
    }

    template<class T, class... Args>
    void Build(T head, Args... tail) 
    { 
        contents.push_back(std::string(head)); 
        Build(tail...);
    }

    template<class T>
    void Build(T head)
    { 
        contents.push_back(std::string(head)); 
    }

    void Build() {}        

    std::string title;
    std::vector<std::string> contents;

};

template<class T, class... Args>
std::shared_ptr<T> spnew(Args... args) {
    return std::shared_ptr<T>(new T(args...));
}

int main(int argc, char** argv) {
    auto succeeds = spnew<Example>("foo", "bar");
    auto fails = spnew<Example>("foo", "bar", "poo", "doo");

    std::cout << "succeeds->contents contains..." << std::endl;
    for ( auto s : succeeds->contents ) std::cout << s << std::endl;

    std::cout << std::endl << "fails->contents contains..." << std::endl;
    for ( auto s : fails->contents ) std::cout << s << std::endl;
}

尽管泛型模板是类型安全的,但编译器会抱怨contents.push_back如果传递的类型不能转换为const char *.

如上所述,您的代码在 gcc 4.6 上运行良好,但是您收到的警告在此处解释了 why-doesnt-my-template-accept-an-initializer-list,并且可能不符合标准,尽管 c++0x 标准尚未发布,因此这可能会改变。

于 2011-07-05T09:32:45.203 回答
0

使用 gcc-4.7(可能也适用于 gcc-4.6,只是分支)并带有警告:

foo.cpp: In function ‘int main(int, char**)’:
foo.cpp:29:47: warning: deducing ‘Args ...’ as ‘std::initializer_list<const 
char*>’ [enabled by default]
foo.cpp:22:20: warning:   in call to ‘std::shared_ptr<_Tp1> spnew(Args ...) 
[with T = Example, Args = {const char*, std::initializer_list<const 
char*>}]’ [enabled by default]
foo.cpp:29:47: warning:   (you can disable this with -fno-deduce-init-list) 
[enabled by default]

我不确定为什么有人会想对 init-list 扣除提出异议。

有一个相关的线程: 为什么我的模板不接受初始化列表

基本上,一个简单的初始化列表没有类型。

于 2011-04-27T04:01:26.050 回答