2

Right, I KNOW this is possible in C++0x/C++11, and therefore can be done in Visual Studio 2012.

HOWEVER, I am running Visual Studio 2010. I want to know if it is at all possible to do something similar to:

void MyFunction(int myArray[])
{}

MyFunction({1,2,3});

Without the inconvenience of having to declare the array beforehand.

Is there any workaround way of doing this in the version of C++ Visual Studio 2010 uses? Or somehow updating the compiler Visual Studio 2010 uses to support more C++11 features? Or am I out of options?

EDIT:

Thanks to yzt, I have been able to do this with Boost!

Here's some example code in case anyone else happens to be in my position (I don't seem to be able to use a normal array with this, but an std::vector (or indeed another stl container), etc will do just fine!):

The function:

void TestFunction(std::vector<int> myArray)
{
    for(std::vector<int>::size_type i = 0; i < myArray.size(); ++i)
    {
        std::cout<<myArray[i]<<std::endl;
    }
}

Calling it:

TestFunction(boost::assign::list_of(1)(2)(3));

8
  • possible duplicate of What C++11 features does Visual Studio 2010 support? Commented Aug 15, 2013 at 22:47
  • why is this not working? Commented Aug 15, 2013 at 22:49
  • This is not supported in VC11 (VS2012) either, but it is available in VC12 (VS2013 Preview.) Commented Aug 15, 2013 at 22:50
  • I seem to remember that Boost had a library for something in this ballpark (assigning to STL containers.) Commented Aug 15, 2013 at 22:53
  • I didn't know this. In C#, you can do something like int[] a = new int[]{1, 2, 3};, but not in C++!? Commented Aug 15, 2013 at 23:00

4 Answers 4

2

The only version (yet) of Visual C++ to support this directly is the Visual C++ 2013 Preview.

If you're really set on doing this, you can define an vector_builder class that will let you do the job in one line -- with a few provisos: first that it pretty much does have to be a vector instead of an array, and second that the syntax to do the job is quite ugly and counter-intuitive (to the point that I hesitate to even mention it at all).

template<class T>
class make_vector {
    std::vector<T> data;
public:
    make_vector(T const &val) { 
        data.push_back(val);
    }

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

    operator std::vector<T>() { return data; }
};

template<class T> 
make_vector<T> makeVect(T const &t) { 
    return make_vector<T>(t);
}

With this, you'd call something like:

MyFunction((makeVect(1),2,3));

As I said, this strikes me as ugly enough that I hesitate to mention it at all -- but you may prefer it to the available alternatives.

Sign up to request clarification or add additional context in comments.

2 Comments

I think there is a single extra open parenthesis in your sample usage code (before 1.) It should be ((makeVect(1),2,3)), I believe.
Plus 1 for pointing out how ugly this type of coding style is.
1

I'm not sure about your exact use case, but you probably can use Boost.Assign to do this. And yes, it does work in VC10.

As has been posted in the comments, one can do like this:

TestFunction(boost::assign::list_of(1)(2)(3));

1 Comment

TestFunction(boost::assign::list_of(1)(2)(3)); THANK YOU! :D
0

Not possible in Visual Studio 2010, or in 2012, for that matter. Sorry!

Comments

0

The MSDN page outlining C++11 support in Visual Studio states that initialiser lists are not supported in VC10 or VC11.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.