int x[6] = {1, 2, 3, 4, 5, 6}
이러한 구문은 이미 MSVC2010
methodName({1, 2, 3, 4, 5, 6})
에 의해 supprted됩니다하지만이 지원되지 않습니다. 그리고 위에서 언급 한 것처럼 "int const (&) [6]"함수 매개 변수에만 의미가 있습니다.
methodName(make_array(1,2,3,4,5,6));
Example implementation :
#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;
}
또는 with help of Boost.Preprocessor :
가 Intead는 다음 구문을 사용할 수
#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;
}
의 INT (X) [6] = {1, 2, 3, 4, 5, 6}; 잘 작동합니다. – harper
'methodName'의 선언은 무엇입니까? 이것이 의미있는 유일한 방법은 인수가'int const (&) [6]'(또는 이것의 템플릿 버전)이고, C++ 11에서만 그렇다. –
@KerrekSB in C++ 11 벡터, std :: array 또는 그 구문을 사용하는 많은 것을 취하는 함수를 호출 할 수 있습니다. – bames53