explicit template args

N

Noah Roberts

I'm trying to override boost::lexical_cast for a particular type. I
think I'm doing it right but it isn't compiling. Says "illegal use of
explicit template arguments".

Code:
namespace boost {
template <typename Source>
esi::utility::strange_double
lexical_cast<esi::utility::strange_double, Source>(Source in)
{
return lexical_cast<double>(in);
}
}

strange_double has an implicit constructor so the return should be ok.

A similar function also doesn't compile:

template < typename unit_t, typename type_t, typename Source >
esi::units::quantity<unit_t, type_t>
lexical_cast<esi::units::quantity<unit_t, type_t>, Source >(Source in)
{
return lexical_cast<type_t>(in) * unit_t();
}

Use of cast is something like:

quantity<pressure, strange_double> q = boost::lexical_cast<
quantity<pressure, strange_double> >(argv[0]);

What am I doing wrong? Help me understand the flaw in my understanding
of templates that is causing this error. The website for the compiler
error isn't much help, it doesn't seem to speak to what my code is
actually attempting...it seems to me that it should work still.

http://msdn2.microsoft.com/en-us/library/e338tex6(VS.80).aspx

Thanks.
 
B

Barry

Noah said:
I'm trying to override boost::lexical_cast for a particular type. I

actually lexical_cast is not virtual class member function, so, no
override. only overload.
think I'm doing it right but it isn't compiling. Says "illegal use of
explicit template arguments".

Code:
namespace boost {
template <typename Source>
esi::utility::strange_double
lexical_cast<esi::utility::strange_double, Source>(Source in)
{
return lexical_cast<double>(in);
}
}

strange_double has an implicit constructor so the return should be ok.

A similar function also doesn't compile:

the function above compiles??
template < typename unit_t, typename type_t, typename Source >
esi::units::quantity<unit_t, type_t>
lexical_cast<esi::units::quantity<unit_t, type_t>, Source >(Source in)

lexical_cast(Source in)

explicit template argument list is only allowed for function full
specialization
{
return lexical_cast<type_t>(in) * unit_t();
}

Use of cast is something like:

quantity<pressure, strange_double> q = boost::lexical_cast<
quantity<pressure, strange_double> >(argv[0]);

What am I doing wrong? Help me understand the flaw in my understanding
of templates that is causing this error. The website for the compiler
error isn't much help, it doesn't seem to speak to what my code is
actually attempting...it seems to me that it should work still.

http://msdn2.microsoft.com/en-us/library/e338tex6(VS.80).aspx

Thanks.
 
C

Cholo Lennon

I'm trying to override boost::lexical_cast for a particular type. I
think I'm doing it right but it isn't compiling. Says "illegal use of
explicit template arguments".

Code:
namespace boost {
template <typename Source>
esi::utility::strange_double
lexical_cast<esi::utility::strange_double, Source>(Source in)
{
return lexical_cast<double>(in);
}

}

strange_double has an implicit constructor so the return should be ok.

A similar function also doesn't compile:

template < typename unit_t, typename type_t, typename Source >
esi::units::quantity<unit_t, type_t>
lexical_cast<esi::units::quantity<unit_t, type_t>, Source >(Source in)
{
return lexical_cast<type_t>(in) * unit_t();
}

Use of cast is something like:

quantity<pressure, strange_double> q = boost::lexical_cast<
quantity<pressure, strange_double> >(argv[0]);

What am I doing wrong? Help me understand the flaw in my understanding
of templates that is causing this error. The website for the compiler
error isn't much help, it doesn't seem to speak to what my code is
actually attempting...it seems to me that it should work still.

http://msdn2.microsoft.com/en-us/library/e338tex6(VS.80).aspx

Thanks.


The right technique is to provide an insert operator for your special
type, something like this:

#include <string>
#include <iostream>
#include <boost/lexical_cast.hpp>

using namespace std;

struct SomeType
{
std::string strValue;
};

// You have to provide this function
std::eek:stream& operator<<(std::eek:stream& rSs, const SomeType& type)
{
// The trick is to insert your type into stream
rSs << type.strValue;
return rSs;
}


int main(int, char*[])
{
using namespace std;

SomeType type;
type.strValue = "12.56";

double result = boost::lexical_cast<double>(type);
cout << result << endl;

return 0;
}


If you provide operator<< you don't have to modify lexical_cast

Best regards
 
C

Cholo Lennon

I'm trying to override boost::lexical_cast for a particular type. I
think I'm doing it right but it isn't compiling. Says "illegal use of
explicit template arguments".
Code:
namespace boost {
template <typename Source>
esi::utility::strange_double
lexical_cast<esi::utility::strange_double, Source>(Source in)
{
return lexical_cast<double>(in);
}

strange_double has an implicit constructor so the return should be ok.
A similar function also doesn't compile:
template < typename unit_t, typename type_t, typename Source >
esi::units::quantity<unit_t, type_t>
lexical_cast<esi::units::quantity<unit_t, type_t>, Source >(Source in)
{
return lexical_cast<type_t>(in) * unit_t();
}
Use of cast is something like:
quantity<pressure, strange_double> q = boost::lexical_cast<
quantity<pressure, strange_double> >(argv[0]);
What am I doing wrong? Help me understand the flaw in my understanding
of templates that is causing this error. The website for the compiler
error isn't much help, it doesn't seem to speak to what my code is
actually attempting...it seems to me that it should work still.

Thanks.

The right technique is to provide an insert operator for your special
type, something like this:

#include <string>
#include <iostream>
#include <boost/lexical_cast.hpp>


struct SomeType
{
std::string strValue;

};

// You have to provide this function
std::eek:stream& operator<<(std::eek:stream& rSs, const SomeType& type)
{
// The trick is to insert your type into stream
rSs << type.strValue;
return rSs;

}

int main(int, char*[])
{
using namespace std;

SomeType type;
type.strValue = "12.56";

double result = boost::lexical_cast<double>(type);
cout << result << endl;

return 0;

}

If you provide operator<< you don't have to modify lexical_cast

Best regards

Upps I forgot something... if you want to convert your type to other
different to those supported by C++ streams you have to provide an
extraction operator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top