Board index » cppbuilder » Re: passing a const to a template method

Re: passing a const to a template method


2005-12-01 07:37:55 PM
cppbuilder85
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 {*word*88}s 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.
 
 

Re:Re: 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 {*word*88}s 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]
info.borland.com/newsgroups/guide.html
 

Re: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<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)
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)
 

{smallsort}

Re:Re: passing a const to a template method

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?
qc.borland.com/wc/qcmain.aspx
qc.borland.com/wc/qcmain.aspx
qc.borland.com/wc/qcmain.aspx
qc.borland.com/wc/qcmain.aspx
--
AlisdairM(TeamB)
 

Re:Re: passing a const to a template method

On Thu, 1 Dec 2005 11:51:54 +0000, "Andrue Cope [TeamB]"
< XXXX@XXXXX.COM >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
 

Re:Re: passing a const to a template method

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 {*word*88}s 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.
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...
Tom
 

Re: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]
info.borland.com/newsgroups/guide.html
 

Re: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]
info.borland.com/newsgroups/guide.html
 

Re:Re: passing a const to a template method

Alisdair Meredith [TeamB] wrote:
Quote
Tom Widmer wrote:


>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?

qc.borland.com/wc/qcmain.aspx
qc.borland.com/wc/qcmain.aspx
qc.borland.com/wc/qcmain.aspx
qc.borland.com/wc/qcmain.aspx
Err, yes, exactly like those. :)
Tom
 

Re:Re: passing a const to a template method

"Alisdair Meredith [TeamB]" < XXXX@XXXXX.COM >writes:
Quote
#include <boost/type_traits.hpp>

template<typename TValueType>
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;
?