Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template function that accepts const and non-const

Reply
Thread Tools

template function that accepts const and non-const

 
 
u.int.32.t
Guest
Posts: n/a
 
      04-23-2007
On Apr 23, 12:42 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> 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.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-24-2007
u.int.32.t wrote:
> On Apr 23, 12:42 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
>>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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57