473,698 Members | 2,615 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 2466
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
2234
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 compile time. The same source code compile if I don't try to split it up into separate libraries. ...
13
2489
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
2440
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
3495
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, which accepts not only sequences but iterators. Is there any place in the language that still...
6
8793
by: denis | last post by:
What are the benefits of using const functions? How does the compiler interpret const functions? Thanks, Denis
4
2021
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
3159
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 inside the function. And it is for this reason that some people advocate that it is a good idea to...
4
6690
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
1235
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 class FirstOne { public int intTry;
39
2783
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 when I should?
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8905
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.