Compilation errors in templated code

Discussion in 'C++' started by Saulius, Jan 17, 2004.

  1. Saulius

    Saulius Guest

    template<template<typename T> class StoragePolicy>
    class construction_info {
    // Empty by default, specialize for other types.
    };

    template<template<typename> class StoragePolicy,
    typename ConversionPolicy = DefaultConversions>
    class Settings
    {
    public:

    // Construct from construction_info.
    Settings(const construction_info<StoragePolicy> &info)
    : policy_(info)
    {
    policy_.Read();
    }

    private:

    // A copy of a constructed data policy.
    StoragePolicy<ConversionPolicy> policy_;
    };

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    template<typename ConversionPolicy>
    class Xml
    {
    public:

    // Typedef for quicker typing.
    typedef construction_info<Xml> ctor_info; // ERROR!?

    // Construct from ctor_info (definition after the class).
    Xml(const ctor_info &info)
    : c_info_(info) { }

    void Read() {
    std::cout << "XMLREAD" << std::endl;
    }

    private:
    ctor_info c_info_; // Construction info
    };

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    template<>
    class construction_info<Xml>
    {
    public:
    construction_info(const std::string &filepath)
    : filepath_(filepath) {}

    std::string filepath_;
    };

    typedef construction_info<Xml> make_xml;

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Settings<Xml> xml( make_xml("D:\\file.xml") );

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    // DJGPP compiles everything, but msvc.net2k3
    // issues this:
    //
    // d:\code\tests\sett\xml.hpp(19): error C3200:
    // 'Xml<ConversionPolicy>' : invalid template argument
    // for template parameter 'StoragePolicy', expected a class template
    //
    // d:\code\tests\sett\xml.hpp(19): error C3200:
    // 'Xml<ConversionPolicy>' : invalid template argument
    // for template parameter 'StoragePolicy', expected a class template
    // with
    // [
    // ConversionPolicy=DefaultConversions
    // ]
    //

    Any suggestions?
     
    Saulius, Jan 17, 2004
    #1
    1. Advertising

  2. Saulius

    Jeff Schwab Guest

    Saulius wrote:
    > template<template<typename T> class StoragePolicy>


    What's that supposed to mean?

    > class construction_info {
    > // Empty by default, specialize for other types.
    > };


    ....

    > Any suggestions?


    ITYM:

    template< typename StoragePolicy >
    class construction_info {
    // Empty by default, specialize for other types.
    };
     
    Jeff Schwab, Jan 17, 2004
    #2
    1. Advertising

  3. Saulius

    Jeff Schwab Guest

    Jeff Schwab wrote:
    > Saulius wrote:
    >
    >> template<template<typename T> class StoragePolicy>

    >
    >
    > What's that supposed to mean?
    >
    >> class construction_info {
    >> // Empty by default, specialize for other types.
    >> };

    >
    >
    > ...
    >
    >> Any suggestions?

    >
    >
    > ITYM:
    >
    > template< typename StoragePolicy >
    > class construction_info {
    > // Empty by default, specialize for other types.
    > };
    >


    Augh! Ignore that, please. :(

    Your code compiles just fine with GCC 3.3.
     
    Jeff Schwab, Jan 17, 2004
    #3
  4. Saulius wrote in news::

    > template<typename ConversionPolicy>
    > class Xml
    > {
    > public:
    >
    > // Typedef for quicker typing.
    > typedef construction_info<Xml> ctor_info; // ERROR!?


    typedef construction_info< ::Xml > ctor_info;

    Within the declaration of this class template Xml is the type
    Xml< ConversionPolicy > *not* the template, the explicit
    namespace qualification fixes that.


    >
    > // Construct from ctor_info (definition after the class).
    >


    Note that the space in "< ::Xml" is important, without it you
    get a di/trigraph and therfore some wierd compilation error.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jan 17, 2004
    #4
  5. Saulius

    Saulius Guest

    Rob Williscroft <> wrote in message news:<Xns9473CC01683ukcoREMOVEfreenetrtw@195.129.110.131>...
    > Saulius wrote in news::
    >
    > > template<typename ConversionPolicy>
    > > class Xml
    > > {
    > > public:
    > >
    > > // Typedef for quicker typing.
    > > typedef construction_info<Xml> ctor_info; // ERROR!?

    >
    > typedef construction_info< ::Xml > ctor_info;
    >
    > Within the declaration of this class template Xml is the type
    > Xml< ConversionPolicy > *not* the template, the explicit
    > namespace qualification fixes that.
    >
    >
    > >
    > > // Construct from ctor_info (definition after the class).
    > >

    >
    > Note that the space in "< ::Xml" is important, without it you
    > get a di/trigraph and therfore some wierd compilation error.
    >
    > Rob.


    Now it won`t let me specialize construction_info for Xml:

    d:\code\tests\sett\xml.hpp(44): error C2908: explicit specialization;
    'settings::construction_info<StoragePolicy>' has already been
    instantiated
    with
    [
    StoragePolicy=settings::Xml
    ]
    d:\code\tests\sett\xml.hpp(50): error C2766: explicit specialization;
    'settings::construction_info<settings::Xml>' has already been defined

    (settings is the namespace in witch everything`s written)
     
    Saulius, Jan 18, 2004
    #5
  6. Saulius wrote in news::

    > Now it won`t let me specialize construction_info for Xml:
    >
    > d:\code\tests\sett\xml.hpp(44): error C2908: explicit specialization;
    > 'settings::construction_info<StoragePolicy>' has already been
    > instantiated
    > with
    > [
    > StoragePolicy=settings::Xml
    > ]
    > d:\code\tests\sett\xml.hpp(50): error C2766: explicit specialization;
    > 'settings::construction_info<settings::Xml>' has already been defined
    >
    > (settings is the namespace in witch everything`s written)
    >


    Ok I actually spotted this the first time, but there were
    other errors (DefaultConversions, std::cout, std::endl),
    that I assumed, wrongly, had something to do with it.

    CBuilder preview (an EDG front end) gives the error a bit
    more legably than VC7.1 : explicit specialization of class
    .... must precede its first use.

    I had to put:

    #include <iostream>
    #include <ostream>
    #include <string>

    class DefaultConversions {};

    /* from original code ...
    */
    template<template<typename T> class StoragePolicy>
    class construction_info {
    // Empty by default, specialize for other types.
    };

    /* forward declare the Xml template
    */
    template <typename ConversionPolicy> class Xml;

    /* Moved forward ...
    */
    template<>
    class construction_info< Xml >
    {
    public:
    construction_info(const std::string &filepath)
    : filepath_(filepath) {}

    std::string filepath_;
    };

    As the begining of your posted code to fix this, remove the
    specialization from the end ofcourse :).


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jan 18, 2004
    #6
  7. Saulius

    Jumbo Guest

    "Saulius" <> wrote in message
    news:...
    > template<template<typename T> class StoragePolicy>
    > class construction_info {
    > // Empty by default, specialize for other types.
    > };
    >
    > template<template<typename> class StoragePolicy,
    > typename ConversionPolicy = DefaultConversions>
    > class Settings
    > {
    > public:
    >
    > // Construct from construction_info.
    > Settings(const construction_info<StoragePolicy> &info)
    > : policy_(info)
    > {
    > policy_.Read();
    > }
    >
    > private:
    >
    > // A copy of a constructed data policy.
    > StoragePolicy<ConversionPolicy> policy_;
    > };
    >
    > // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > template<typename ConversionPolicy>
    > class Xml
    > {
    > public:
    >
    > // Typedef for quicker typing.
    > typedef construction_info<Xml> ctor_info; // ERROR!?


    Note you are defining a templated class so the template parameters are
    deduced when you instantiate the class.
    simply remove the template parameters i.e:
    typedef construction_info ctor_info;

    This should compile fine then if you include the <string> header for
    std::string.

    HTH.
     
    Jumbo, Jan 19, 2004
    #7
  8. Saulius

    Jumbo Guest

    "Jumbo @uko2.co.uk>" <pcr1000011<nospam> wrote in message
    news:...
    >
    > "Saulius" <> wrote in message
    > news:...
    > > template<template<typename T> class StoragePolicy>
    > > class construction_info {
    > > // Empty by default, specialize for other types.
    > > };
    > >
    > > template<template<typename> class StoragePolicy,
    > > typename ConversionPolicy = DefaultConversions>
    > > class Settings
    > > {
    > > public:
    > >
    > > // Construct from construction_info.
    > > Settings(const construction_info<StoragePolicy> &info)
    > > : policy_(info)
    > > {
    > > policy_.Read();
    > > }
    > >
    > > private:
    > >
    > > // A copy of a constructed data policy.
    > > StoragePolicy<ConversionPolicy> policy_;
    > > };
    > >
    > > // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > > template<typename ConversionPolicy>
    > > class Xml
    > > {
    > > public:
    > >
    > > // Typedef for quicker typing.
    > > typedef construction_info<Xml> ctor_info; // ERROR!?

    >
    > Note you are

    oops typedeffing I mean
    a templated class so the template parameters are
    > deduced when you instantiate the class.
    > simply remove the template parameters i.e:
    > typedef construction_info ctor_info;
    >
    > This should compile fine then if you include the <string> header for
    > std::string.
    >
    > HTH.
    >

    How did this end up here? :-s
     
    Jumbo, Jan 19, 2004
    #8
    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. RA Scheltema
    Replies:
    3
    Views:
    424
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    489
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,283
  4. Amadeus W. M.
    Replies:
    2
    Views:
    415
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    1
    Views:
    320
Loading...

Share This Page