explicit template args

Discussion in 'C++' started by Noah Roberts, Aug 22, 2007.

  1. Noah Roberts

    Noah Roberts Guest

    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.
    Noah Roberts, Aug 22, 2007
    #1
    1. Advertising

  2. Noah Roberts

    Barry Guest

    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/library/e338tex6(VS.80).aspx
    >
    > Thanks.




    --
    Thanks
    Barry
    Barry, Aug 22, 2007
    #2
    1. Advertising

  3. Noah Roberts

    Cholo Lennon Guest

    On Aug 21, 8:01 pm, Noah Roberts <> 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/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

    --
    Cholo Lennon
    Bs.As.
    ARG
    Cholo Lennon, Aug 22, 2007
    #3
  4. Noah Roberts

    Cholo Lennon Guest

    On Aug 22, 11:41 am, Cholo Lennon <> wrote:
    > On Aug 21, 8:01 pm, Noah Roberts <> 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/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>
    >
    >
    > 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
    >
    > --
    > 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
    Cholo Lennon, Aug 22, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Varn
    Replies:
    2
    Views:
    624
    Ken Varn
    Jun 22, 2005
  2. J.T. Conklin
    Replies:
    1
    Views:
    438
    David Hilsee
    Aug 11, 2004
  3. Replies:
    3
    Views:
    491
    David Eppstein
    Sep 17, 2003
  4. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    686
    Pierre Fortin
    Sep 18, 2004
  5. er
    Replies:
    2
    Views:
    504
Loading...

Share This Page