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
16 2475
Steven T. Hatton wrote: 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];
when reading C++ (and C) types you need to start from the identifier.
aPlus is a function that takes parameters t1 and t2 returning a
reference to an array of length S of T1. 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]; ?
This is the same as the previous one except that t2 is a const T2.
Steven T. Hatton wrote: 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]; ?
Sorry I meant to say "and takes arguments of reference to array of type T1
and reference to array of type (const) T2", not returns ... const ...
--
"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
Gianni Mariani wrote: Steven T. Hatton wrote: type const T2: T1 (&aPlus(T1(&t 1)[S],const T2(&t2)[S]))[S]; ?
This is the same as the previous one except that t2 is a const T2.
That's what I expected.
T a0[3] = {10,20,30};
T a1[3] = {1,2,3};
T(&ar)[3]=a1;
const T(&car)[3]=a1;
aPlus(a0,car);//works
aPlus<ORDER,T,c onst*T>(a0,car) ; //works
aPlus<ORDER,T,T >(a0,car); // won't compile
I would expect the last call to be treated as:
aPlus(T(&a0)[3], const T(&a1)[3]))[3];
I think my confusion is that I don't really understand what the template
parameters are doing.
--
"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 wrote:
.... That's what I expected. T a0[3] = {10,20,30}; T a1[3] = {1,2,3}; T(&ar)[3]=a1; const T(&car)[3]=a1;
aPlus(a0,car);//works aPlus<ORDER,T,c onst T>(a0,car); //works aPlus<ORDER,T,T >(a0,car); // won't compile
I would expect the last call to be treated as: aPlus(T(&a0)[3], const T(&a1)[3]))[3];
I think my confusion is that I don't really understand what the template parameters are doing.
Assuming that this is your funtion aPlus.
template <unsigned ORDER, typename T1, typename T2 >
T1 (&aPlus(T1(&t 1)[ORDER], T2(&t2)[ORDER]))[ORDER]
What you're doing is when you instantiate aPlus<ORDER,T,T > is creating a
function like:
T (&aPlus(T(&t1 )[ORDER], T(&t2)[ORDER]))[ORDER];
Then you try to pass "car", which is a const T array to a non const T
array reference. The compiler shoul not compile that.
Gianni Mariani wrote: Steven T. Hatton wrote: ... That's what I expected. T a0[3] = {10,20,30}; T a1[3] = {1,2,3}; T(&ar)[3]=a1; const T(&car)[3]=a1;
aPlus(a0,car);//works aPlus<ORDER,T,c onst T>(a0,car); //works aPlus<ORDER,T,T >(a0,car); // won't compile
I would expect the last call to be treated as: aPlus(T(&a0)[3], const T(&a1)[3]))[3];
I think my confusion is that I don't really understand what the template parameters are doing.
Assuming that this is your funtion aPlus.
template <unsigned ORDER, typename T1, typename T2 > T1 (&aPlus(T1(&t 1)[ORDER], T2(&t2)[ORDER]))[ORDER]
What you're doing is when you instantiate aPlus<ORDER,T,T > is creating a function like:
T (&aPlus(T(&t1 )[ORDER], T(&t2)[ORDER]))[ORDER];
Then you try to pass "car", which is a const T array to a non const T array reference. The compiler shoul not compile that.
I now realize that I didn't actually reproduce the problem I thought I had.
The original code wouldn't compile with or without the const. Below is the
code that was originally causing problems. I had been using:
typedef T1 (&rank1T1)[ORDER];
typedef T2 (&rank1T2)[ORDER];
static rank1T1 result(rank1T1 a1, const rank1T2 a2)
template < unsigned REMAINING, unsigned ORDER, typename T1, typename T2,
typename OP>
class Array2OpAssign {
public:
typedef T1 (&rank1T1)[ORDER];
typedef const T2 (&rank1T2)[ORDER];
static rank1T1 result(rank1T1 a1, rank1T2 a2) {
OP op;
a1[ORDER - REMAINING] = op(a1[ORDER - REMAINING], a2[ORDER - REMAINING]);
return Array2OpAssign< REMAINING - 1, ORDER, T1, T2, OP>::result(a1, a2);
}
};
template < unsigned ORDER, typename T1, typename T2, typename OP>
class Array2OpAssign< 1, ORDER, T1, T2, OP> {
public:
typedef T1 (&rank1T1)[ORDER];
typedef const T2 (&rank1T2)[ORDER];// *NOTE* *DIFFERENCE*
static rank1T1 result (rank1T1 a1, rank1T2 a2){//*NOTE* *DIFFERENCE*
OP op;
a1[ORDER - 1] = op(a1[ORDER - 1], a2[ORDER - 1]);
return a1;
}
};
template <unsigned ORDER, typename T1, typename T2 >
T1 (&array2PlusAss ign(T1(&t1)[ORDER], const T2(&t2)[ORDER]))[ORDER]
{
return Array2OpAssign< ORDER,ORDER, T1, T2, Plus<T1, T2> >::result(t1,
t2);
}
template <unsigned ORDER, typename T1, typename T2 >
T1 (&array2MinusAs sign(T1(&t1)[ORDER], T2(&t2)[ORDER]))[ORDER]
{
return Array2OpAssign< ORDER,ORDER, T1, T2, Minus<T1, T2> >::result(t1,
t2);
}
}
}
/*************** *************** *************** *************** ***************
* Copyright (C) 2004 by Steven T. Hatton *
* ha*****@globals ymmetry.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
*************** *************** *************** *************** ***************/
--
"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 wrote:
....
Does this mean you no longer have a problem ?
.... I now realize that I didn't actually reproduce the problem I thought I had. The original code wouldn't compile with or without the const. Below is the code that was originally causing problems. I had been using: typedef T1 (&rank1T1)[ORDER]; typedef T2 (&rank1T2)[ORDER]; static rank1T1 result(rank1T1 a1, const rank1T2 a2)
template < unsigned REMAINING, unsigned ORDER, typename T1, typename T2, typename OP> class Array2OpAssign { public: typedef T1 (&rank1T1)[ORDER]; typedef const T2 (&rank1T2)[ORDER];
static rank1T1 result(rank1T1 a1, rank1T2 a2) { OP op; a1[ORDER - REMAINING] = op(a1[ORDER - REMAINING], a2[ORDER - REMAINING]); return Array2OpAssign< REMAINING - 1, ORDER, T1, T2, OP>::result(a1, a2); } };
template < unsigned ORDER, typename T1, typename T2, typename OP> class Array2OpAssign< 1, ORDER, T1, T2, OP> { public: typedef T1 (&rank1T1)[ORDER]; typedef const T2 (&rank1T2)[ORDER];// *NOTE* *DIFFERENCE*
static rank1T1 result (rank1T1 a1, rank1T2 a2){//*NOTE* *DIFFERENCE* OP op; a1[ORDER - 1] = op(a1[ORDER - 1], a2[ORDER - 1]); return a1; } };
template <unsigned ORDER, typename T1, typename T2 > T1 (&array2PlusAss ign(T1(&t1)[ORDER], const T2(&t2)[ORDER]))[ORDER] { return Array2OpAssign< ORDER,ORDER, T1, T2, Plus<T1, T2> >::result(t1, t2); }
template <unsigned ORDER, typename T1, typename T2 > T1 (&array2MinusAs sign(T1(&t1)[ORDER], T2(&t2)[ORDER]))[ORDER] { return Array2OpAssign< ORDER,ORDER, T1, T2, Minus<T1, T2> >::result(t1, t2); } } }
Gianni Mariani wrote: Steven T. Hatton wrote: ...
Does this mean you no longer have a problem ?
That's actually a funny question. :D My problems number in the transfinite.
I now have code that performs as I would like it to. I learned a lot from
this experience, and your guidance was invaluable. Thank you...and Victor
too.
I'm trying to read _C++_Templates_ :A_Complete_Gui de_, but I'm finding it
very difficult. Everytime I look in the book I get new ideas I want to
try, so I'm back to the keyboard and stumbling around for hours before I
turn the page.
--
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
...
|
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 ) {}
|
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;
|
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...
|
by: denis |
last post by:
What are the benefits of using const functions?
How does the compiler interpret const functions?
Thanks, Denis
| |
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();
|
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...
|
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
|
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;
|
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?
|
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,...
| |
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...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |