Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > explicit template args

Reply
Thread Tools

explicit template args

 
 
Noah Roberts
Guest
Posts: n/a
 
      08-21-2007
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/lib...x6(VS.80).aspx

Thanks.
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      08-22-2007
Noah Roberts wrote:
> 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/lib...x6(VS.80).aspx
>
> Thanks.




--
Thanks
Barry
 
Reply With Quote
 
 
 
 
Cholo Lennon
Guest
Posts: n/a
 
      08-22-2007
On Aug 21, 8:01 pm, Noah Roberts <(E-Mail Removed)> wrote:
> 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/lib...x6(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:stream& operator<<(std: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

--
Cholo Lennon
Bs.As.
ARG





 
Reply With Quote
 
Cholo Lennon
Guest
Posts: n/a
 
      08-22-2007
On Aug 22, 11:41 am, Cholo Lennon <(E-Mail Removed)> wrote:
> On Aug 21, 8:01 pm, Noah Roberts <(E-Mail Removed)> wrote:
>
>
>
> > 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/lib...x6(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>
>
>
> struct SomeType
> {
> std::string strValue;
>
> };
>
> // You have to provide this function
> std:stream& operator<<(std: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
>
> --
> 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.

--
Cholo Lennon
Bs.As.
ARG



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++0x -- fun(Args&...) and fun(Args const&...) er C++ 2 12-20-2010 07:52 PM
Is there a class or method to construct url args or extract url args? Ken Varn ASP .Net 2 06-22-2005 12:26 PM
args v. *args passed to: os.path.join() Pierre Fortin Python 2 09-18-2004 06:59 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM
When passing functions as args,how to pass extra args for passed function? python@sarcastic-horse.com Python 3 09-17-2003 12:25 AM



Advertisments