| View previous topic :: View next topic |
| Author |
Message |
Alisdair Meredith [TeamB] Guest
|
Posted: Thu Dec 01, 2005 11:37 am Post subject: Re: passing a const to a template method |
|
|
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
|
Posted: Thu Dec 01, 2005 11:51 am Post subject: passing a const to a template method |
|
|
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
|
Posted: Thu Dec 01, 2005 12:15 pm Post subject: Re: passing a const to a template method |
|
|
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
|
|
| Back to top |
|
 |
Zara Guest
|
Posted: Thu Dec 01, 2005 12:38 pm Post subject: Re: passing a const to a template method |
|
|
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
|
Posted: Thu Dec 01, 2005 1:56 pm Post subject: Re: passing a const to a template method |
|
|
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
|
Posted: Thu Dec 01, 2005 2:09 pm Post subject: Re: passing a const to a template method |
|
|
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
|
Posted: Thu Dec 01, 2005 8:44 pm Post subject: Re: passing a const to a template method |
|
|
"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 |
|
 |
|