Can temporary array as argument.

Discussion in 'C++' started by Peng Yu, Sep 12, 2008.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    It seems that an temporary can not be an argument. Since the last two
    statements below does not work. I have to have declare one array and
    then initialized an A object with the array, which is cumbersome. I'm
    wondering if there is any walkaround, or I have to until the new C++
    standard?

    Thanks,
    Peng

    #include <vector>
    #include <iostream>

    template <typename T>
    class A {
    public:
    template <size_t N>
    A(const T(&v)[N]) {
    for(size_t i = 0; i < N; ++ i)
    _v.push_back(v);
    }
    private:
    std::vector<T> _v;
    };

    int main() {
    double v[] = {0, 1, 2, 3 };
    A<double> a1(v);
    A<double> a2 = v;
    A<double> a3 = {0, 1, 2, 3};//error
    A<double> a4({0, 1, 2, 3});//error
    }
    Peng Yu, Sep 12, 2008
    #1
    1. Advertising

  2. Peng Yu

    mqrk Guest

    On Sep 11, 4:48 pm, Peng Yu <> wrote:
    > Hi,
    >
    > It seems that an temporary can not be an argument. Since the last two
    > statements below does not work. I have to have declare one array and
    > then initialized an A object with the array, which is cumbersome. I'm
    > wondering if there is any walkaround, or I have to until the new C++
    > standard?
    >
    > Thanks,
    > Peng
    >
    > #include <vector>
    > #include <iostream>
    >
    > template <typename T>
    > class A {
    > public:
    > template <size_t N>
    > A(const T(&v)[N]) {
    > for(size_t i = 0; i < N; ++ i)
    > _v.push_back(v);
    > }
    > private:
    > std::vector<T> _v;
    >
    > };
    >
    > int main() {
    > double v[] = {0, 1, 2, 3 };
    > A<double> a1(v);
    > A<double> a2 = v;
    > A<double> a3 = {0, 1, 2, 3};//error
    > A<double> a4({0, 1, 2, 3});//error
    >
    > }


    I thought of a workaround, but requires enormous amounts of repetitive
    code.

    //Basically it would look like this in use:

    A<double> a( bx(0.0, 1.0, 2.0, 3.0).a );

    //And it would be implemented like this:

    template <class, int> struct ArrayWrapper;

    template <class T>
    struct ArrayWrapper<T, 1>
    {
    T a[1];
    ArrayWrapper( T t0 )
    {
    a[0] = t0;
    }
    };

    template <class T>
    struct ArrayWrapper<T, 2>
    {
    T a[2];
    ArrayWrapper( T t0, T t1 )
    {
    a[0] = t0;
    a[1] = t1;
    }
    };

    // etc. up to ArrayWrapper<T, 10> maybe

    template <class T>
    ArrayWrapper< T, 1 > bx( T t0 )
    {
    return ArrayWrapper< T, 1 >( t0 );
    }

    template <class T>
    ArrayWrapper< T, 2 > bx( T t0, T t1 )
    {
    return ArrayWrapper< T, 2 >( t0, t1 );
    }

    //etc.

    I can't imagine the problem is so cumbersome as to warrant this
    monstrosity, but I thought I'd throw it out there for you.
    mqrk, Sep 12, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    410
  2. Ben Kial
    Replies:
    1
    Views:
    642
    Eric Enright
    Nov 15, 2004
  3. Replies:
    1
    Views:
    292
    Victor Bazarov
    Jul 26, 2007
  4. Klaas Vantournhout

    Check if an argument is temporary

    Klaas Vantournhout, Oct 28, 2007, in forum: C++
    Replies:
    17
    Views:
    475
    Klaas Vantournhout
    Oct 31, 2007
  5. Replies:
    7
    Views:
    3,203
    James Kanze
    Feb 12, 2008
Loading...

Share This Page