S
Steven T. Hatton
In the following code, the only way I can figure out to pass an array of
const is by setting the template argument to const in the instanciation
expression. It would be (or seem to me) better if I could set that
qualifier in the function call. Can that be done?
#include <iostream>
using std:
stream;
using std::cout;
template<typename T, unsigned ORDER>
void printArray(T array[], ostream& out=cout)
{
out << "{";
for(unsigned i = 0;i < ORDER; i++ ){
out << array;
if(i < ORDER - 1){ out << ",";}
}
out << "}\n\n";
}
template <unsigned ORDER, typename T1, typename T2 >
T1 (&aPlus(T1(&t1)[ORDER], T2(&t2)[ORDER]))[ORDER]
{
for(unsigned i = 0;i < ORDER; i++ )
{
t1 += t2;
}
return t1;
}
template <unsigned ORDER, typename T>
void test_aPlus()
{
T a0[ORDER];
T a1[ORDER];
for(unsigned i = 0; i < ORDER; i++)
{
a0=i;
a1=i*i;
}
aPlus<ORDER, T,T>(a0,a1);
printArray<T,5>(a0);
printArray<T,5>(a1);
T(&ar)[ORDER]=a1;
aPlus<ORDER, T,T>(a0,ar);
printArray<T,5>(a0);
printArray<T,5>(ar);
const T(&car)[ORDER]=a1;
/**
uncomment the following line and it won't compile
**************************/
//aPlus<ORDER,T,T>(a0,car);
aPlus<ORDER,T,const T>(a0,car);
printArray<T,5>(a0);
printArray<const T,5>(car);
}
int main()
{
test_aPlus<5,float>();
return 0;
}
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell
const is by setting the template argument to const in the instanciation
expression. It would be (or seem to me) better if I could set that
qualifier in the function call. Can that be done?
#include <iostream>
using std:
using std::cout;
template<typename T, unsigned ORDER>
void printArray(T array[], ostream& out=cout)
{
out << "{";
for(unsigned i = 0;i < ORDER; i++ ){
out << array;
if(i < ORDER - 1){ out << ",";}
}
out << "}\n\n";
}
template <unsigned ORDER, typename T1, typename T2 >
T1 (&aPlus(T1(&t1)[ORDER], T2(&t2)[ORDER]))[ORDER]
{
for(unsigned i = 0;i < ORDER; i++ )
{
t1 += t2;
}
return t1;
}
template <unsigned ORDER, typename T>
void test_aPlus()
{
T a0[ORDER];
T a1[ORDER];
for(unsigned i = 0; i < ORDER; i++)
{
a0=i;
a1=i*i;
}
aPlus<ORDER, T,T>(a0,a1);
printArray<T,5>(a0);
printArray<T,5>(a1);
T(&ar)[ORDER]=a1;
aPlus<ORDER, T,T>(a0,ar);
printArray<T,5>(a0);
printArray<T,5>(ar);
const T(&car)[ORDER]=a1;
/**
uncomment the following line and it won't compile
**************************/
//aPlus<ORDER,T,T>(a0,car);
aPlus<ORDER,T,const T>(a0,car);
printArray<T,5>(a0);
printArray<const T,5>(car);
}
int main()
{
test_aPlus<5,float>();
return 0;
}
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell