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.
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.
--
Thanks
Barry
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::ostream& operator<<(std::ostream& 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
--
Cholo Lennon
Bs.As.
ARG
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.