BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

passing a const to a template method

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Alisdair Meredith [TeamB]
Guest





PostPosted: Thu Dec 01, 2005 11:37 am    Post subject: Re: passing a const to a template method Reply with quote



I would look at boost type_traits (to be standardised via Library TR1)

#include <boost/type_traits.hpp>

template<typename TValueType>
TValueType ToNumeric( TValueType ValueOnError )
{
std::wistringstream i( MyString.get() );

boost::remove_const< TValueType >::type x;

if( !( i>>x ) )
return ValueOnError;

return x;
}

AlisdairM(TeamB)


Andrue Cope [TeamB] wrote:

Quote:
I have the following template method:

template<typename TValueType
TValueType ToNumeric( TValueType ValueOnError ) const
{
std::wistringstream i( MyString.get() );

TValueType x; // << E2304 constant must be initialised

if( !( i>>x ) )
return ValueOnError;

return x;
}

Unfortunately this chokes if you pass it a constant ie;

const int WIBBLE=5;
myObject.ToNumeric( WIBBLE );

I suppose what I need is someway to remove the constness from
TValueType but I don't know how I'd do that.

Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Thu Dec 01, 2005 11:51 am    Post subject: passing a const to a template method Reply with quote



I have the following template method:

template<typename TValueType>
TValueType ToNumeric( TValueType ValueOnError ) const
{
std::wistringstream i( MyString.get() );

TValueType x; // << E2304 constant must be initialised

if( !( i>>x ) )
return ValueOnError;

return x;
}

Unfortunately this chokes if you pass it a constant ie;

const int WIBBLE=5;
myObject.ToNumeric( WIBBLE );

I suppose what I need is someway to remove the constness from
TValueType but I don't know how I'd do that.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Alisdair Meredith [TeamB]
Guest





PostPosted: Thu Dec 01, 2005 12:15 pm    Post subject: Re: passing a const to a template method Reply with quote



Alisdair Meredith [TeamB] wrote:

Quote:
I would look at boost type_traits (to be standardised via Library TR1)

#include <boost/type_traits.hpp

template TValueType ToNumeric( TValueType ValueOnError )
{
std::wistringstream i( MyString.get() );

boost::remove_const< TValueType >::type x;

if( !( i>>x ) )
return ValueOnError;

return x;
}

AlisdairM(TeamB)


OK, now I've shown you the general case, the following should be a
simpler solution without any other library dependencies:

template<typename TValueType>
TValueType ToNumeric( const TValueType ValueOnError ) const
{
std::wistringstream i( MyString.get() );

TValueType x; // << E2304 constant must be initialised

if( !( i>>x ) )
return ValueOnError;

return x;
}


In this case the const is in the parameter declaration, and TValueType
should always deduce as non-const (as const const type makes no sense)

Some might prefer to make that a const reference, to avoid expensive
copies. OTOH, I understand your main use case is going to be primitive
types like int, so that pass-by-reference would probably be *less*
efficient. [And obfuscating code with metaprogramming tricks for
optimal passing is simply not worth it in most cases]

AlisdairM(TeamB)

Back to top
Alisdair Meredith [TeamB]
Guest





PostPosted: Thu Dec 01, 2005 12:30 pm    Post subject: Re: passing a const to a template method Reply with quote

Tom Widmer wrote:

Quote:
It shouldn't be a problem in any case - top level const is ignored
during type deduction on non-reference parameters, so TValueType will
be deduced as "int", not "const int". Unless your compiler has a
serious bug...


You mean something like these?

http://qc.borland.com/wc/qcmain.aspx?d=525
http://qc.borland.com/wc/qcmain.aspx?d=666
http://qc.borland.com/wc/qcmain.aspx?d=3200
http://qc.borland.com/wc/qcmain.aspx?d=4013


--

AlisdairM(TeamB)

Back to top
Zara
Guest





PostPosted: Thu Dec 01, 2005 12:38 pm    Post subject: Re: passing a const to a template method Reply with quote

On Thu, 1 Dec 2005 11:51:54 +0000, "Andrue Cope [TeamB]"
<no.spam (AT) not (DOT) a.valid.address> wrote:

Quote:
I have the following template method:

template<typename TValueType
TValueType ToNumeric( TValueType ValueOnError ) const

change to:
TValueType ToNumeric( const TValueType ValueOnError ) const

Quote:
{
std::wistringstream i( MyString.get() );

TValueType x; // << E2304 constant must be initialised

if( !( i>>x ) )
return ValueOnError;

return x;
}


Best regards,

-- Zara

Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Thu Dec 01, 2005 1:56 pm    Post subject: Re: passing a const to a template method Reply with quote

Alisdair Meredith [TeamB] wrote:

Quote:
OK, now I've shown you the general case, the following should be a
simpler solution without any other library dependencies:

You've pointed me at another part of boost anyway so I should probably
take that as a reason to learn it. The solution you've posted works so
that's good.

Now I just have to get back to my refactoring. When is a DWORD not a
DWORD? When Andrue changes it into a QWORD :-/

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Thu Dec 01, 2005 2:09 pm    Post subject: Re: passing a const to a template method Reply with quote

I'm laughing on the outside and crying on the inside.

At least in this case the compiler is giving a meaningful error
message. Try changing my original code slightly:

template<typename TValueType> TValueType
ToNumeric( TValueType ValueOnError ) const
{
std::wistringstream i( MyString.get() );

TValueType x=ValueOnError; // Changed this..

if( !( i>>x ) ) // << E2094
return ValueOnError;

return x;
}

E2094 'operator>>' not implemented in type 'std::wistringstream' for
arguments of type 'unsigned __int64'

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Thu Dec 01, 2005 8:44 pm    Post subject: Re: passing a const to a template method Reply with quote

"Alisdair Meredith [TeamB]" <alisdair.NO.SPAM.PLEASE.meredith (AT) uk (DOT) renaultf1.com> writes:

Quote:
#include <boost/type_traits.hpp

template TValueType ToNumeric( TValueType ValueOnError )
{
std::wistringstream i( MyString.get() );

boost::remove_const< TValueType >::type x;

type is a dependant name, isn't it? Wouldn't this have to be

typename boost::remove_const< TValueType >::type x;

?

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.