C++为可变参数重载运算符逗号

发布于 2024-08-24 07:59:32 字数 381 浏览 14 评论 0原文

是否可以通过重载参数的运算符逗号来构造函数的可变参数?我想看一个例子如何做到这一点......,也许是这样的:

template <typename T> class ArgList {
public:
    ArgList(const T& a);
    ArgList<T>& operator,(const T& a,const T& b);
}
//declaration
void myFunction(ArgList<int> list);

//in use:
myFunction(1,2,3,4);

//or maybe:
myFunction(ArgList<int>(1),2,3,4);

is it possible to construct variadic arguments for function by overloading operator comma of the argument? i want to see an example how to do so.., maybe something like this:

template <typename T> class ArgList {
public:
    ArgList(const T& a);
    ArgList<T>& operator,(const T& a,const T& b);
}
//declaration
void myFunction(ArgList<int> list);

//in use:
myFunction(1,2,3,4);

//or maybe:
myFunction(ArgList<int>(1),2,3,4);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┈┾☆殇 2024-08-31 07:59:32

这是可能的,但用法看起来不太好。例如:

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

template <class T>
class list_of
{
    std::vector<T> data;
public:
    typedef typename std::vector<T>::const_iterator const_iterator;
    const_iterator begin() const { return data.begin(); }
    const_iterator end() const { return data.end(); }

    list_of& operator, (const T& t) {
        data.push_back(t);
        return *this;
    }
};

void print(const list_of<int>& args)
{
    std::copy(args.begin(), args.end(), std::ostream_iterator<int>(std::cout, " "));
}

int main()
{
    print( (list_of<int>(), 1, 2, 3, 4, 5) );
}

这个缺点将在 C++0x 中得到修复,您可以这样做:

void print(const std::initializer_list<int>& args)
{
    std::copy(args.begin(), args.end(), std::ostream_iterator<int>(std::cout, " "));
}

int main()
{
    print( {1, 2, 3, 4, 5} );
}

或者甚至使用混合类型:

template <class T>
void print(const T& t)
{
    std::cout << t;
}

template <class Arg1, class ...ArgN>
void print(const Arg1& a1, const ArgN& ...an)
{
    std::cout << a1 << ' ';
    print(an...);
}


int main()
{
    print( 1, 2.4, 'u', "hello world" );
}

It is sort-of possible, but the usage won't look very nice. For exxample:

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

template <class T>
class list_of
{
    std::vector<T> data;
public:
    typedef typename std::vector<T>::const_iterator const_iterator;
    const_iterator begin() const { return data.begin(); }
    const_iterator end() const { return data.end(); }

    list_of& operator, (const T& t) {
        data.push_back(t);
        return *this;
    }
};

void print(const list_of<int>& args)
{
    std::copy(args.begin(), args.end(), std::ostream_iterator<int>(std::cout, " "));
}

int main()
{
    print( (list_of<int>(), 1, 2, 3, 4, 5) );
}

This shortcoming will be fixed in C++0x where you can do:

void print(const std::initializer_list<int>& args)
{
    std::copy(args.begin(), args.end(), std::ostream_iterator<int>(std::cout, " "));
}

int main()
{
    print( {1, 2, 3, 4, 5} );
}

or even with mixed types:

template <class T>
void print(const T& t)
{
    std::cout << t;
}

template <class Arg1, class ...ArgN>
void print(const Arg1& a1, const ArgN& ...an)
{
    std::cout << a1 << ' ';
    print(an...);
}


int main()
{
    print( 1, 2.4, 'u', "hello world" );
}
梦回旧景 2024-08-31 07:59:32

运算符具有固定数量的参数。你无法改变这一点。逗号运算符有两个参数。所以不。不过,您可以通过一些努力来推出自定义的级联版本。

Operators have a fixed number of parameters. You cannot change that. The comma operator takes two arguments. So no. You can roll a custom, cascading version though, with some effort.

一笔一画续写前缘 2024-08-31 07:59:32

也许是这样的:

class MyArgList {
public:   
     typedef std::list<boost::any> ManyList;

     template <typename T>
     MyArgList& operator, (const T& val) {
        elems.push_back(val);
        return *this; 
     }

     ManyList::iterator begin() {return elems.begin();}
       ...

private:
     ManyList elems;
};

用法是:

void foo(MyArgList& list);
foo((myArgList(),1,2,3,4,5));

Maybe something like this:

class MyArgList {
public:   
     typedef std::list<boost::any> ManyList;

     template <typename T>
     MyArgList& operator, (const T& val) {
        elems.push_back(val);
        return *this; 
     }

     ManyList::iterator begin() {return elems.begin();}
       ...

private:
     ManyList elems;
};

Usage would be:

void foo(MyArgList& list);
foo((myArgList(),1,2,3,4,5));
没有心的人 2024-08-31 07:59:32

不,不是。由逗号运算符分隔的值列表将被视为单个值。例如:

1,2,3

将产生单个值 3。

No, it isn't. The list of values separated by the comma operator will be evaluated as a single value. For example:

1,2,3

will result in a single value, 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文