使t2 const:T1(& aPlus(T1(& t1)[S],T2(& t2)[S]))[S] [英] Make t2 const: T1 (&aPlus(T1(&t1)[S],T2(&t2)[S]))[S]

查看:62
本文介绍了使t2 const:T1(& aPlus(T1(& t1)[S],T2(& t2)[S]))[S]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我可以想出传递一个

const数组的唯一方法是在instanciation

表达式中将template参数设置为const。如果我可以在函数调用中设置

限定符,那么(或者在我看来)会更好。可以这样做吗?

#include< iostream>

使用std :: ostream;

使用std :: cout;


模板< typename T,unsigned ORDER>

void printArray(T array [],ostream& out = cout)

{

out<< " {" ;;

for(unsigned i = 0; i< ORDER; i ++){

out<< array [i];

if(i< ORDER - 1){out<< ",";}

}

out<< "} \ n \ n" ;;

}


模板< unsigned ORDER,typename T1,typename T2>

T1(& aPlus(T1(& t1)[ORDER],T2(& t2)[ORDER]))[ORDER]

{

for(unsigned i = 0; i< ORDER; i ++)

{

t1 [i] + = t2 [i];

}

返回t1;

}


模板< unsigned ORDER,typename T>

void test_aPlus()

{

T a0 [ORDER];

T a1 [ORDER];


for(unsigned i = 0; i< ORDER; i ++)

{

a0 [i] = i;

a1 [i] = 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;


/ * *

取消注释以下行,它不会编译

************************** /

// 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>();

返回0;

}


-

如果我们的假设是关于任何事情而不是关于某一个或多个

特定的东西,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题。 - Bertrand

Russell

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::ostream;
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[i];
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[i] += t2[i];
}
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]=i;
a1[i]=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

推荐答案

Steven T. Hatton写道:
Steven T. Hatton wrote:
在下面的代码中,我可以想出通过一个
const数组的唯一方法是在instanciation
表达式中将template参数设置为const。如果我可以在函数调用中设置
限定符,那么(或者在我看来)会更好。可以这样做吗?
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?




我不确定你到底要做什么,但是你可以超载

aPlus 。



I''m not sure exactly what you''re trying to do, however you can overload
aPlus.


Gianni Mariani写道:
Gianni Mariani wrote:
Steven T. Hatton写道:
Steven T. Hatton wrote:
在下面的代码中,我可以想出传递一个
const数组的唯一方法是在instanciation
表达式中将template参数设置为const。如果我可以在函数调用中设置
限定符,那么(或者在我看来)会更好。可以这样做吗?
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?



我不确定你要做什么,但是你可以超载
aPlus。



I''m not sure exactly what you''re trying to do, however you can overload
aPlus.



您编译并运行代码吗?使用和没有注释行

/ **

*****取消注释* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

**************************** /

** // aPlus< ; ORDER,T,T>(a0,car);




我正在尝试在此函数模板中创建第二个参数const:

模板< unsigned ORDER,typename T1,typename T2>

T1(& aPlus(T1(& t1)[ORDER],** T2(& ; t2)[订购]))[订购];


你知道怎么做吗?

-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的推论就构成了数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题。 - Bertrand

Russell



Did you compile and run the code? With, and without the commented line
/**
*****uncomment*the*following*line*and*it*won''t*com pile
****************************/
**//aPlus<ORDER,T,T>(a0,car);
?

I''m trying to make the second parameter const in this function template:
template <unsigned ORDER, typename T1, typename T2 >
T1 (&aPlus(T1(&t1)[ORDER],**T2(&t2)[ORDER]))[ORDER];

Do you know how to do that?
--
"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


Steven T. Hatton写道:
Steven T. Hatton wrote:
Gianni Mariani写道:

Gianni Mariani wrote:

Steven T. Hatton写道:
Steven T. Hatton wrote:
在下面的代码中,我唯一可以弄清楚传递数组
const是通过在instanciation
表达式中将模板参数设置为const。如果我可以在函数调用中设置
限定符,那么(或者在我看来)会更好。可以这样做吗?
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?



我不确定你要做什么,但是你可以超载
aPlus。



I''m not sure exactly what you''re trying to do, however you can overload
aPlus.


你编译并运行代码了吗?有,没有注释行
/ **
取消注释以下行,它不会编译
***************** ********* /
// aPlus< ORDER,T,T>(a0,car);


我正在努力该函数模板中的第二个参数const:
模板< unsigned ORDER,typename T1,typename T2>
T1(& aPlus(T1(& t1)[ORDER],T2(&) t2)[订购]))[订购];

你知道怎么做吗?


Did you compile and run the code? With, and without the commented line
/**
uncomment the following line and it won''t compile
**************************/
//aPlus<ORDER,T,T>(a0,car);
?

I''m trying to make the second parameter const in this function template:
template <unsigned ORDER, typename T1, typename T2 >
T1 (&aPlus(T1(&t1)[ORDER], T2(&t2)[ORDER]))[ORDER];

Do you know how to do that?




是的。


尝试下面的代码 - 注意重载的aPlus。


另外,你不需要指定模板参数,它们可以是

由编译器推导出来。即


aPlus(a0,汽车); //这不能用于下面的重载

//但是可以使用OP代码。


这就是我很困惑的事情关于,为什么你可以推断出模板

参数?

#include< iostream>

使用std :: ostream ;

使用std :: cout;


模板< typename T,unsigned ORDER>

void printArray(T array [] ,ostream& out = cout)

{

out<< " {" ;;

for(unsigned i = 0; i< ORDER; i ++){

out<< array [i];

if(i< ORDER - 1){out<< ",";}

}

out<< "} \ n \ n" ;;

}


模板< unsigned ORDER,typename T1,typename T2>

T1(& aPlus(T1(& t1)[ORDER],T2(& t2)[ORDER]))[ORDER]

{

for(unsigned i = 0; i< ORDER; i ++)

{

t1 [i] + = t2 [i];

}

返回t1;

}


模板< unsigned ORDER,typename T1,typename T2>

T1(& aPlus(T1(& t1)[ORDER],const T2(& t2)[ORDER]))[ORDER]

{

for(unsigned i = 0; i< ORDER; i ++)

{

t1 [i] + = t2 [i];

}

返回t1;

}


模板< unsigned ORDER,typename T>

void test_aPlus()

{

T a0 [ORDER];

T a1 [ORDER];

for(unsigned i = 0; i< ORDER; i ++)

{

a0 [i] = i;

a1 [i] = i * i;

}


aPlus< ORDER,T,T>(a0,a1);

printArr ay< 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;


/ **

取消注释以下行,它不会编译

*********************** *** /

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>();

返回0;

}



Yes.

Try the code below - note the overloaded aPlus.

Also, you don''t need to specify the template parameters, they can be
deduced by the compiler. i.e.

aPlus(a0,car); // this won''t work with the overloading below
// but will work with the OP code.

That''s what I am confused about, why are you specifying the template
parameters when they can be deduced ?
#include <iostream>
using std::ostream;
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[i];
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[i] += t2[i];
}
return t1;
}

template <unsigned ORDER, typename T1, typename T2 >
T1 (&aPlus(T1(&t1)[ORDER], const T2(&t2)[ORDER]))[ORDER]
{
for(unsigned i = 0;i < ORDER; i++ )
{
t1[i] += t2[i];
}
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]=i;
a1[i]=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;
}


这篇关于使t2 const:T1(&amp; aPlus(T1(&amp; t1)[S],T2(&amp; t2)[S]))[S]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆