Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Compilation errors in templated code (http://www.velocityreviews.com/forums/t280709-compilation-errors-in-templated-code.html)

Saulius 01-17-2004 06:25 PM

Compilation errors in templated code
 
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?

Jeff Schwab 01-17-2004 06:38 PM

Re: Compilation errors in templated code
 
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 01-17-2004 06:48 PM

Re: Compilation errors in templated code
 
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.


Rob Williscroft 01-17-2004 08:04 PM

Re: Compilation errors in templated code
 
Saulius wrote in news:a3897071.0401171025.30b2d53e@posting.google.c om:

> 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/

Saulius 01-18-2004 10:41 AM

Re: Compilation errors in templated code
 
Rob Williscroft <rtw@freenet.REMOVE.co.uk> wrote in message news:<Xns9473CC01683ukcoREMOVEfreenetrtw@195.129.1 10.131>...
> Saulius wrote in news:a3897071.0401171025.30b2d53e@posting.google.c om:
>
> > 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)

Rob Williscroft 01-18-2004 04:45 PM

Re: Compilation errors in templated code
 
Saulius wrote in news:a3897071.0401180241.64f37563@posting.google.c om:

> 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/

Jumbo 01-19-2004 12:13 AM

Re: Compilation errors in templated code
 

"Saulius" <saulis@balticum-tv.lt> wrote in message
news:a3897071.0401171025.30b2d53e@posting.google.c om...
> 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 01-19-2004 12:17 AM

Re: Compilation errors in templated code
 

"Jumbo @uko2.co.uk>" <pcr1000011<nospam> wrote in message
news:1074471812.890065@news.minx.net.uk...
>
> "Saulius" <saulis@balticum-tv.lt> wrote in message
> news:a3897071.0401171025.30b2d53e@posting.google.c om...
> > 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






All times are GMT. The time now is 08:35 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.