473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

template function that accepts const and non-const

Is there a way to make a single template function that will accept
both const and non-const parameter types by reference without making a
copy?

For example, I can write this which will accept both, but will always
make a copy:

template<typename T1>
void accept(T1 t);

Thanks for your time.

Apr 22 '07 #1
11 9541
u.int.32.t wrote:
Is there a way to make a single template function that will accept
both const and non-const parameter types by reference without making a
copy?

For example, I can write this which will accept both, but will always
make a copy:

template<typename T1>
void accept(T1 t);

Thanks for your time.
Use a reference:

template<typename T1>
void accept(T1& t);

Apr 22 '07 #2
On Apr 22, 10:23 am, Rolf Magnus <ramag...@t-online.dewrote:
u.int.32.t wrote:
Is there a way to make a single template function that will accept
both const and non-const parameter types by reference without making a
copy?
For example, I can write this which will accept both, but will always
make a copy:
template<typename T1>
void accept(T1 t);
Thanks for your time.

Use a reference:

template<typename T1>
void accept(T1& t);
Yes but this won't work if accept is called as:

accept(5);

For example.

Apr 22 '07 #3
u.int.32.t wrote:
Is there a way to make a single template function that will accept
both const and non-const parameter types by reference without making a
copy?

For example, I can write this which will accept both, but will always
make a copy:

template<typename T1>
void accept(T1 t);

Thanks for your time.
Why would you want to? If you want to accept both const and non-const
parameters by reference, the function can't modify them. So just use

template <typename T1>
void accept(const T1& t);

--
Ian Collins.
Apr 22 '07 #4
u.int.32.t wrote:
....
>Use a reference:

template<typename T1>
void accept(T1& t);

Yes but this won't work if accept is called as:

accept(5);

For example.
Then overload accept.

template <typename T>
void accept( T & x );

template <typename T>
void accept( const T & x );

int main()
{
int i;

accept( i );
accept( 5 );
}
.... if you really want to live on the thin edge implement the const
version like this:

template <typename T>
void accept( const T & x )
{
T t(x);
accept(t);
}

.... don't do this unless you are sure that no-one will misuse it as
strange things will happen if you expect modifications to the parameter
to mean anything.
Apr 22 '07 #5
On Apr 22, 4:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
u.int.32.t wrote:
Is there a way to make a single template function that will accept
both const and non-const parameter types by reference without making a
copy?
For example, I can write this which will accept both, but will always
make a copy:
template<typename T1>
void accept(T1 t);
Thanks for your time.

Why would you want to? If you want to accept both const and non-const
parameters by reference, the function can't modify them. So just use

template <typename T1>
void accept(const T1& t);
accept was a forwarding function which I now realize can't be done
fully without changes to the language. And it really did need to allow
mutable references.

Apr 23 '07 #6
u.int.32.t wrote:
On Apr 22, 4:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
>u.int.32.t wrote:
>>Is there a way to make a single template function that will accept
both const and non-const parameter types by reference without making a
copy?
For example, I can write this which will accept both, but will always
make a copy:
template<typename T1>
void accept(T1 t);
Thanks for your time.
Why would you want to? If you want to accept both const and non-const
parameters by reference, the function can't modify them. So just use

template <typename T1>
void accept(const T1& t);

accept was a forwarding function which I now realize can't be done
fully without changes to the language. And it really did need to allow
mutable references.

NO NO .... be careful with mutable. In some cases it makes perfect
sense, in some it does not, it really depends on if the mutable elements
are part of the value or part of the management. e.g. A cache should be
mutable (because I should be able to change what things are cached even
if it's a const object), however the data in the cache should not be
mutable.

Anyhow, what was wrong with my overloaded method suggestion ?
Apr 23 '07 #7

"Gianni Mariani" <gi*******@mariani.wswrote in message
news:46***********************@per-qv1-newsreader-01.iinet.net.au...
u.int.32.t wrote:
>On Apr 22, 4:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>Why would you want to? If you want to accept both const and non-const
parameters by reference, the function can't modify them. So just use

template <typename T1>
void accept(const T1& t);

accept was a forwarding function which I now realize can't be done
fully without changes to the language. And it really did need to allow
mutable references.


NO NO .... be careful with mutable. In some cases it makes perfect sense,
in some it does not, it really depends on if the mutable elements are part
of the value or part of the management. e.g. A cache should be mutable
(because I should be able to change what things are cached even if it's a
const object), however the data in the cache should not be mutable.
Uhm, I think you're misinterpreting u.int.32.t :). What he's saying has
nothing to do with the C++ keyword 'mutable', but with the meaning of the
word mutable itself, e.g., the function needs to be allowed to make changes
to the entity referenced to. From what I understand of it, he has to write a
forwarding function that is able to forward it's parameters as-is to another
(probably yet undefined) function. If you define the accept function taking
a const T&, it can't forward to functions taking a T&, and if you define
accept() taking a T&, you can't pass it a const T&. So without a change in
the language, overloading is the only way possible (C++09 will have "r-value
references" that, amongst other things, allow you to forward passed-in
function parameters to other functions as-is)

- Sylvester
Apr 23 '07 #8
On Apr 23, 8:45 am, "Sylvester Hesp" <s.h...@oisyn.nlwrote:
"Gianni Mariani" <gi3nos...@mariani.wswrote in message

news:46***********************@per-qv1-newsreader-01.iinet.net.au...
u.int.32.t wrote:
On Apr 22, 4:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
Why would you want to? If you want to accept both const and non-const
parameters by reference, the function can't modify them. So just use
>template <typename T1>
void accept(const T1& t);
accept was a forwarding function which I now realize can't be done
fully without changes to the language. And it really did need to allow
mutable references.
NO NO .... be careful with mutable. In some cases it makes perfect sense,
[snip]
Uhm, I think you're misinterpreting u.int.32.t :). What he's saying has
nothing to do with the C++ keyword 'mutable', but with the meaning of the
word mutable itself, e.g., the function needs to be allowed to make changes
to the entity referenced to. From what I understand of it, he has to write a
forwarding function that is able to forward it's parameters as-is to another
(probably yet undefined) function. If you define the accept function taking
a const T&, it can't forward to functions taking a T&, and if you define
accept() taking a T&, you can't pass it a const T&. So without a change in
the language, overloading is the only way possible (C++09 will have "r-value
references" that, amongst other things, allow you to forward passed-in
function parameters to other functions as-is)
Yes, this is exactly it.

For now, I am probably going to do the equivalent of:

template<typename T>
void accept(T & t); // T=const T when necessary

and in the cases where I call accept like:

accept(someRvalueFromFunction())

I'll change it to:

accept(static_cast<const T&>(someRvalueFromFunction());

How does that sound?

Apr 23 '07 #9
u.int.32.t wrote:
>
For now, I am probably going to do the equivalent of:

template<typename T>
void accept(T & t); // T=const T when necessary

and in the cases where I call accept like:

accept(someRvalueFromFunction())

I'll change it to:

accept(static_cast<const T&>(someRvalueFromFunction());

How does that sound?
Why can't you just overload accept for const?

--
Ian Collins.
Apr 23 '07 #10
On Apr 23, 12:42 pm, Ian Collins <ian-n...@hotmail.comwrote:
u.int.32.t wrote:
For now, I am probably going to do the equivalent of:
template<typename T>
void accept(T & t); // T=const T when necessary
and in the cases where I call accept like:
accept(someRvalueFromFunction())
I'll change it to:
accept(static_cast<const T&>(someRvalueFromFunction());
How does that sound?

Why can't you just overload accept for const?
Because accept is in really an polymorphic n-ary forwarding function.
I think it would be prohibitive to have overloads for each
permutation.

If it were just accept(T1), that would work, but its really
accept(T1,T2,T3,T4,...) etc, etc.

Apr 23 '07 #11
u.int.32.t wrote:
On Apr 23, 12:42 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>u.int.32.t wrote:

>>>For now, I am probably going to do the equivalent of:
>>>template<typename T>
void accept(T & t); // T=const T when necessary
>>>and in the cases where I call accept like:
>>>accept(someRvalueFromFunction())
>>>I'll change it to:
>>>accept(static_cast<const T&>(someRvalueFromFunction());
>>>How does that sound?

Why can't you just overload accept for const?


Because accept is in really an polymorphic n-ary forwarding function.
I think it would be prohibitive to have overloads for each
permutation.

If it were just accept(T1), that would work, but its really
accept(T1,T2,T3,T4,...) etc, etc.
I see. I still think you are taking a risk mixing const and non-const
parameters. You'll have to find a way of making sure a const object
does not get passed to a function expecting a non-const.

--
Ian Collins.
Apr 24 '07 #12

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

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
3
by: ogtindeed | last post by:
Hi all, I need some help. I am trying to instantiate a Template and call a member function, but have not been able - Now this is what I am trying to do: foo.h =========
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
1
by: Patrick Rammelt | last post by:
Hi After upgrading my compiler (gcc-3.3.3 to gcc-4.0.0) I stumbled over an error that I do not understand. Maybe it is a compiler bug, but maybe I can get some new insights from this. Here is a...
4
by: Neelesh | last post by:
Hi all, I had some confusion about deduction of non-type template parameters for function templates : template <class T, int i> void foo (T, int p = i) ; void bar() { foo<int, 10>(40,40);...
6
by: jiake2007 | last post by:
Hi all, I'm trying to write a template class that calls a function from one of its template parameters: template <class T> class Foo { public: Foo(T t): t(t) {}
4
by: Alan Woodland | last post by:
I've been trying out more template metaprogramming ideas with typelists (mostly for personal learning, I'm aware boost most probably provides this facility already), and I've run into this small...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
by: Klaus | last post by:
Hi all, I have some questions for template definition: In the code bellow I could write: #1: GenEditor<Mul(&vint, 5); which compiles fine. But for my understanding Mul is not a typename,...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.