Compilation errors in templated code

S

Saulius

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?
 
J

Jeff Schwab

Saulius said:
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.
};
 
J

Jeff Schwab

Jeff said:
What's that supposed to mean?



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.
 
R

Rob Williscroft

Saulius wrote in
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.
 
S

Saulius

Rob Williscroft said:
Saulius wrote in

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.



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)
 
R

Rob Williscroft

Saulius wrote in
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.
 
J

Jumbo

Saulius said:
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.
 
J

Jumbo

Jumbo @uko2.co.uk> said:
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
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top