473,569 Members | 2,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Make t2 const: T1 (&aPlus(T1(&t 1)[S],T2(&t2)[S]))[S]

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<typena me 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(&t 1)[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,c onst T>(a0,car);

printArray<T,5> (a0);
printArray<cons t T,5>(car);
}

int main()
{
test_aPlus<5,fl oat>();
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

Jul 22 '05 #1
16 2446
Steven T. Hatton wrote:
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?


I'm not sure exactly what you're trying to do, however you can overload
aPlus.
Jul 22 '05 #2
Gianni Mariani wrote:
Steven T. Hatton wrote:
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?


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


Did you compile and run the code? With, and without the commented line
/**
*****uncomment* the*following*l ine*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(&t 1)[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

Jul 22 '05 #3
Steven T. Hatton wrote:
Gianni Mariani wrote:

Steven T. Hatton wrote:
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?


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

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(&t 1)[ORDER], T2(&t2)[ORDER]))[ORDER];

Do you know how to do that?


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<typena me 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(&t 1)[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(&t 1)[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,c onst T>(a0,car);

printArray<T,5> (a0);
printArray<cons t T,5>(car);
}

int main()
{
test_aPlus<5,fl oat>();
return 0;
}
Jul 22 '05 #4
Gianni Mariani wrote:
Steven T. Hatton wrote:
Gianni Mariani wrote:
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.
I don't understand what you mean by OP code.
That's what I am confused about, why are you specifying the template
parameters when they can be deduced ?

I don't fully understand how that works. In most circumstance where I've
been using templates I've had to specify the actual template parameters
(arguments).

#include <iostream>
using std::ostream;
using std::cout;

template<typena me 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(&t 1)[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(&t 1)[ORDER], const T2(&t2)[ORDER]))[ORDER]
{
for(unsigned i = 0;i < ORDER; i++ )
{
t1[i] += t2[i];
}
return t1;
}

OK. I see what you're saying. I guess I was confused by the fact that very
similar code using normal array notation without the references didn't
require any overloading. I guess I've never hit the overloading issue from
this direction before.
--
"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

Jul 22 '05 #5
Steven T. Hatton wrote:
Gianni Mariani wrote:


#include <iostream>
using std::ostream;
using std::cout;

template<typena me 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(&t 1)[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(&t 1)[ORDER], const T2(&t2)[ORDER]))[ORDER]
{
for(unsigned i = 0;i < ORDER; i++ )
{
t1[i] += t2[i];
}
return t1;
}

OK. I see what you're saying. I guess I was confused by the fact that
very similar code using normal array notation without the references
didn't
require any overloading. I guess I've never hit the overloading issue
from this direction before.


I'm still confused about what this means. My understanding of making a
parameter const is that the function won't modify it. That's what I want.
Normally, I can pass a non-const variable as a const reference.

void cfun( const string& cstr)
{
cout << cstr;
}

int main()
{

string str = "This is a non-const variable\";
cfun(str);

}

So why should I have to overload that function?
--
"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

Jul 22 '05 #6
Steven T. Hatton wrote:

I'm still confused about what this means. My understanding of making a
parameter const is that the function won't modify it. That's what I want.
Normally, I can pass a non-const variable as a const reference.

void cfun( const string& cstr)
{
cout << cstr;
}

int main()
{

string str = "This is a non-const variable\";
cfun(str);

}

So why should I have to overload that function?


In the OP (original post) example, it shows a const object being
assigned to a non const reference, this is not allowed.

C++ can deduce template parameters in function templates AND you can
overload functions which becomes a rather rich area of the language with
all kinds of rope to hang ourselves.

e.g.

template < typename T1, typename T2 > void f1( const T1 &, const T2 & );
template < typename T1, typename T2 > void f2( T1 &, T2 & );

struct X; struct Y;
const X & x;
Y & y;
f1( x, y ); // ==> T1 is X, T2 is Y
f2( x, y ); // ==> T1 is const X, T2 is Y
Jul 22 '05 #7
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote:
Steven T. Hatton wrote:

template <unsigned ORDER, typename T1, typename T2 >
T1 (&aPlus(T1(&t 1)[ORDER], const T2(&t2)[ORDER]))[ORDER]
{
}


I'm still confused about what this means. My understanding of making a
parameter const is that the function won't modify it. That's what I want.


I think what you want is for the referred-to things to be const,
not the parameter being const. In fact it is not permitted
to make a reference const (because they cannot be reassigned anyway).
Normally, I can pass a non-const variable as a const reference.

void cfun( const string& cstr)
That is a reference to const string (not a const reference to string).

If you still don't see the difference then consider:
void foo(int *const ptr)
{ *ptr = 2; }
vs.
void foo(int const *ptr)
{ int const y = 2; ptr = &y; }
In the first one, the parameter is const but the pointed-to things
are not; in the second one, it is the other way around.

In your case it is even worse as 'const' could be applied either
to the entire array, or to its members.
int main()
{
string str = "This is a non-const variable\";
cfun(str);
}

So why should I have to overload that function?


The type 'array of const T2' is not the same as 'const (array of T2)'.
Jul 22 '05 #8
Gianni Mariani wrote:
Steven T. Hatton wrote:

I'm still confused about what this means. My understanding of making a
parameter const is that the function won't modify it. That's what I
want. Normally, I can pass a non-const variable as a const reference.

void cfun( const string& cstr)
{
cout << cstr;
}

int main()
{

string str = "This is a non-const variable\";
cfun(str);

}

So why should I have to overload that function?
In the OP (original post) example, it shows a const object being
assigned to a non const reference, this is not allowed.


template <unsigned ORDER, typename T1, typename T2 >
T1 (&aPlus(T1(&t 1)[ORDER], const T2(&t2)[ORDER]))[ORDER]
{
for(unsigned i = 0;i < ORDER; i++ )
{
t1[i] += t2[i];
}
return t1;
}

aPlus(a0,car); // this works
aPlus<ORDER,T,c onst T>(a0,car); // this works
aPlus<ORDER,T,T >(a0,car); // this doesn't

Why isn't car treated as const in the last statement?
C++ can deduce template parameters in function templates AND you can
overload functions which becomes a rather rich area of the language with
all kinds of rope to hang ourselves.

e.g.

template < typename T1, typename T2 > void f1( const T1 &, const T2 & );
template < typename T1, typename T2 > void f2( T1 &, T2 & );

struct X; struct Y;
const X & x;
Y & y;
f1( x, y ); // ==> T1 is X, T2 is Y
f2( x, y ); // ==> T1 is const X, T2 is Y


I'm not following this code. Is it what you had intended to write?

--
"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

Jul 22 '05 #9
Old Wolf wrote:
I think what you want is for the referred-to things to be const,
not the parameter being const. In fact it is not permitted
to make a reference const (because they cannot be reassigned anyway).


Sorry, I should have worded that better. I do understand that distinction.
So why should I have to overload that function?


The type 'array of const T2' is not the same as 'const (array of T2)'.


The following says aPlus is a function that returns a reference to an array
of type T2:
T1 (&aPlus(T1(&t 1)[S],T2(&t2)[S]))[S];

Does this say aPlus is a function that returns a reference to an array of
type const T2:
T1 (&aPlus(T1(&t 1)[S],const T2(&t2)[S]))[S];
?
--
"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

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2221
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at...
13
2478
by: matthias_k | last post by:
Hi, I've never thought about this before, but since you are able to overload a function only by changing the const-ness of the formal parameters, some annoying side effects will arise. For example, if you only have this code ... void foo( const int n ) {}
6
2429
by: bill | last post by:
I recently realized that I have a structure that I'd like to put more protection on in the following sense: I'd like to modify struct foo{ int *x; } ; to be: struct foo { const int *x;
66
3466
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the values side. IIRC, "apply" used to require that the second argument be a tuple; it now accepts sequences, and has been depreciated in favor of *args,...
6
8788
by: denis | last post by:
What are the benefits of using const functions? How does the compiler interpret const functions? Thanks, Denis
4
2015
by: robinsand | last post by:
Header File: car.h #if !defined CAR_H #define CAR_H enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize, ctMiniVan, ctSUV }; class Car { public: Car();
16
3145
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed...
4
6677
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
8
1233
by: neelsmail | last post by:
Hi, I have just started learning C# ; I was working in C++ till now (and would like to if allowed, but learning new things dont hurt). I am used to specifiying "const" to get the assurance that unless someone specifically cast-away the constness, object wont change. So, as part of my first ever program I wrote a following class - public...
39
2762
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I read somewhere that it when you won't modify after its initialization... So when exactly do I use one or another? Is it *wrong* not use const...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.