有没有办法在旧版本的 c++ 中实现初始值设定项列表(有点)(在 c++11 之前)



我试图实现(有点:)旧版 C++ 中的初始值设定项列表。像下面一样..

#include <vector>
#include <cstdarg>
#include <iostream>
using namespace std;
template <class T>
vector <T> VEC_VA(size_t argCount,T arg1, ... )
{
    va_list arguments;
    vector <T> sList;
    va_start ( arguments, argCount);
    for ( int x = 0; x < (int)argCount; x++ )
        sList.push_back(va_arg ( arguments, T));
    va_end ( arguments );
    return sList;
}
struct Test
{
    Test(int _x,std::string _s):x(_x),s(_s){}
    int x;
    std::string s;
};
void methodWithArgs(vector<Test> testObjs)
{
    cout<<"Size:"<<testObjs.size()<<endl;
    for (std::vector<Test>::const_iterator it = testObjs.begin();it != testObjs.end();++it)
        std::cout << it->x <<":"<< it->s.c_str()<< std::endl;
}
int main()
{
    methodWithArgs(VEC_VA(2,Test(1,"one"),Test(2,"two")));
    return 0;
}

但是这种实现在Visual Studio 2012(v11.0(中有效。但不是在 Linux g++ 编译器中。并以错误结尾:"无法通过'...'传递非平凡可复制类型的'结构测试'的对象">

那么还有更好的主意吗?

如果您愿意接受稍微不同的语法,那么您所需要的只是一个返回对向量的引用的 vector::push_back() 版本。像这样:

#include <vector>
#include <iostream>
template<typename T>
class VEC_VA_HELP {
   std::vector<T> vector;
public:
   VEC_VA_HELP(T arg1) : vector() { vector.push_back(arg1); }
   VEC_VA_HELP &operator()(T arg) { vector.push_back(arg); return *this; }
   operator std::vector<T> &() { return vector; }
};
template<typename T>
VEC_VA_HELP<T> VEC_VA(T arg1) { return VEC_VA_HELP<T>(arg1); }
struct Test {
   Test(int _x) : x(_x) {}
   int x;
};
void methodWithArgs(std::vector<Test> const &testObjs) {
   std::cout << testObjs.size() << std::endl;
   for (std::vector<Test>::const_iterator it = testObjs.begin();
        it != testObjs.end();
        ++it)
      std::cout << it->x << std::endl;
}
int
main(void)
{
   methodWithArgs(VEC_VA(Test(1))(Test(2))(Test(3)));
   return 0;
}

还想到了递归模板,但我不确定它们的语法会有多干净。另请参阅有关重载逗号运算符的问题。

和上面一样 丹尼尔的回答 但是一些改进

template <class T>
class VECS
{
    vector <T> vec;
    public:
        VECS(){}
        VECS(T t){vec.push_back(t);}
        VECS& operator()(const T& t){vec.push_back(t);return *this;}
        typedef VECS<T> myVECS;
        myVECS& operator<< (const T& t){vec.push_back(t);return *this;}
        operator vector<T>() const {return vec;}
};

用法

    methodWithArgs(VECS<Test>(Test(1,"one"))(Test(2,"two")));
    methodWithArgs(VECS<Test>()<<Test(1,"one")<<Test(2,"two"));
    methodWithArgs(VECS<Test>(Test(0,"zero")) <<Test(1,"one")<<Test(2,"two"));
    std::vector<int> v1 = VECS<int>() << 1 << 2 << 3;
    std::vector<int> v2 = VECS<int>(1)(2)(3);

相关内容

最新更新