C++ visualc&x2B+;数组文字

C++ visualc&x2B+;数组文字,c++,arrays,visual-studio-2010,visual-c++,literals,C++,Arrays,Visual Studio 2010,Visual C++,Literals,我在windows中工作,我想通过netbeans创建我的程序,所以我需要通过cygwin使用linux编译器。到目前为止,我已经能够使用ints的文本数组,如下所示: int x[6] = {1, 2, 3, 4, 5, 6} 或者用一种方法 methodName({1, 2, 3, 4, 5, 6}) 我最近改成了VisualStudio,但我不被允许这样做。还有别的选择吗 int x[6] = {1, 2, 3, 4, 5, 6} MSVC2010已经支持这种语法 methodNam

我在windows中工作,我想通过netbeans创建我的程序,所以我需要通过cygwin使用linux编译器。到目前为止,我已经能够使用
int
s的文本数组,如下所示:

int x[6] = {1, 2, 3, 4, 5, 6}
或者用一种方法

methodName({1, 2, 3, 4, 5, 6})
我最近改成了VisualStudio,但我不被允许这样做。还有别的选择吗

int x[6] = {1, 2, 3, 4, 5, 6}
MSVC2010已经支持这种语法

methodName({1, 2, 3, 4, 5, 6})
但这是不受支持的。如上所述,它仅适用于“int const(&)[6]”函数参数

Intead您可以使用以下语法:

methodName(make_array(1,2,3,4,5,6));
:

#包括
#包括
使用名称空间std;
样板
结构数组
{
类型定义T类型[大小];
可变类型数据;
数组()
{

cout int x[6]={1,2,3,4,5,6};效果很好
methodName
的声明是什么?唯一有意义的方法是如果参数是
int const(&)[6]
(或其模板版本),甚至只有在C++11中。@KerrekSB在C++11中,人们可以使用该语法调用采用向量、std::数组或许多东西的函数。
#include <iostream>
#include <ostream>

using namespace std;


template<typename T,unsigned size>
struct Array
{
    typedef T type[size];
    mutable type data;
    Array()
    {
        cout << "Array::Array" << endl;
    }
    ~Array()
    {
        cout << "Array::~Array" << endl;
    }
};

template<typename T> inline
typename Array<T,1>::type &make_array(const T &p1,const Array<T,1> &aux=Array<T,1>())
{
    aux.data[0]=p1;
    return aux.data;
}

template<typename T> inline
typename Array<T,2>::type &make_array(const T &p1,const T &p2,const Array<T,2> &aux=Array<T,2>())
{
    aux.data[0]=p1;
    aux.data[1]=p2;
    return aux.data;
}

template<typename T> inline
typename Array<T,3>::type &make_array(const T &p1,const T &p2,const T &p3,const Array<T,3> &aux=Array<T,3>())
{
    aux.data[0]=p1;
    aux.data[1]=p2;
    aux.data[2]=p3;
    return aux.data;
}

// ...

void test_array(int (&p)[3])
{
    cout << p[0] << " " << p[1] << " " << p[2] << endl;
}

void test_ptr(int *p)
{
    cout << p[0] << " " << p[1] << " " << p[2] << endl;
}

int main(int argc,char *argv[])
{
    test_array(make_array(33,22,11));
    test_ptr(make_array(33,22,11));
    return 0;
}
#include <iostream>
#include <ostream>
using namespace std;

// ______________________________________________________________

template<typename T,unsigned size>
struct Array
{
    typedef T type[size];
    mutable type values;
    Array()
    {
        cout << "Array::Array" << endl;
    }
    ~Array()
    {
        cout << "Array::~Array" << endl;
    }
};

#include <boost/preprocessor/iteration/local.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>

#define MAKE_ARRAY_PP_MAX_ARG_COUNT 16

#define MAKE_ARRAY_PP_PARAM_LIST(z, n, data) const T & BOOST_PP_CAT(p, n)
#define MAKE_ARRAY_PP_PARAM_ASSIGN(z, n, data) aux.values[n] = BOOST_PP_CAT(p, n);

#define BOOST_PP_LOCAL_MACRO(n) \
template<typename T> inline \
typename Array<T,n>::type &make_array(BOOST_PP_ENUM(n, MAKE_ARRAY_PP_PARAM_LIST, _) , const Array<T,n> &aux=Array<T,n>()) \
{ \
    BOOST_PP_REPEAT(n, MAKE_ARRAY_PP_PARAM_ASSIGN, _) \
    return aux.values; \
} \
/**/
#define BOOST_PP_LOCAL_LIMITS (1, MAKE_ARRAY_PP_MAX_ARG_COUNT)
#include BOOST_PP_LOCAL_ITERATE()

// ______________________________________________________________

void test_array(int (&p)[3])
{
    cout << p[0] << " " << p[1] << " " << p[2] << endl;
}

void test_ptr(int *p)
{
    cout << p[0] << " " << p[1] << " " << p[2] << endl;
}

int main(int argc,char *argv[])
{
    test_array(make_array(33,22,11));
    test_ptr(make_array(33,22,11));
    make_array(33,22,11,00,55,44,66);
    make_array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
    return 0;
}