Is this valid ISO C++?

I

Ioannis Vranos

This compiles with MINGW GCC 3.4.2 but not with my other compilers. Is it valid ISO C++?


#include <vector>


template< template<class> class ContainerTemplate, class ValueType>
inline void test(const ValueType &val)
{
ContainerTemplate<ValueType> container(val);
}



int main()
{
using namespace std;

test<vector>(10);
}
 
V

Victor Bazarov

Ioannis Vranos said:
This compiles with MINGW GCC 3.4.2 but not with my other compilers. Is it
valid ISO C++?


#include <vector>


template< template<class> class ContainerTemplate, class ValueType>
inline void test(const ValueType &val)
{
ContainerTemplate<ValueType> container(val);
}



int main()
{
using namespace std;

test<vector>(10);
}

No, this is not valid for a simple reason that std::vector is a template
that has more than one argument. Your 'ContainerTemplate' template
template argument has only one argument itself.

Until we have template typedefs you're probably out of luck. You could
try declaring your own template deriving form 'vector<T>', but you'd
have to duplicate all functionality.

V
 
I

Ioannis Vranos

Victor said:
No, this is not valid for a simple reason that std::vector is a template
that has more than one argument. Your 'ContainerTemplate' template
template argument has only one argument itself.

Until we have template typedefs you're probably out of luck. You could
try declaring your own template deriving form 'vector<T>', but you'd
have to duplicate all functionality.


What I am trying to do is passing a template class (not an instance) to a template
function, and create instances of it inside the function. Something like this:

template <class T>
class SomeContainer{};

template <template<class> class ContainerType>
inline void somefunc()
{
ContainerType<int> obj;
}


int main()
{
somefunc<SomeContainer>();
}



This compiles in all my compilers. What has it do with how many arguments vector takes?
 
P

Pete Becker

Ioannis said:
template <template<class> class ContainerType>
inline void somefunc()
{
ContainerType<int> obj;
}
[...]
This compiles in all my compilers. What has it do with how many
arguments vector takes?

The declaration of somefunc says that you'll pass it a template that
takes one argument. vector takes two. To use vector you'd have to
declare somefunc as template<template<class,class> class ContainerType> ...

Except that that doesn't necessarily work, either, because vector can,
hypothetically, take additional implementation-specific arguments.
 
P

Peter MacMillan

Ioannis said:
This compiles in all my compilers. What has it do with how many
arguments vector takes?

I looked at the OP and I was wondering the same thing.

vector is defined as: template <class T, class Allocator = allocator>

So, in your function template (to accept vector) you'd have to have
something like:
//---
#include <memory> // for allocator
#include <vector>

template< template<class, class> class CT, class VT, class AT>
inline void test(const VT &val)
{
CT<VT, AT> container(val);
}

int main()
{
using namespace std;

typedef allocator<int> intalloc;
test<vector, int, intalloc>(10);

}
//---

weird stuff (that you can't ignore auto-template parameters)...


--
Peter MacMillan
e-mail/msn: (e-mail address removed)
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)
 
P

Peter MacMillan

//---
#include <memory>
#include <vector>

template< template<class, class> class CT, class VT>
inline void test(const VT &val)
{
CT<VT, std::allocator<VT> > container(val);
}

int main()
{
using namespace std;
test<vector, int>(10);
}
//---

Sorry for the extra post. Was just bugged that I didn't need that extra
template parameter. ;0

--
Peter MacMillan
e-mail/msn: (e-mail address removed)
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)
 
S

Stefan Strasser

Ioannis said:
This compiles with MINGW GCC 3.4.2 but not with my other compilers. Is
it valid ISO C++?


#include <vector>


template< template<class> class ContainerTemplate, class ValueType>
inline void test(const ValueType &val)
{
ContainerTemplate<ValueType> container(val);
}



int main()
{
using namespace std;

test<vector>(10);
}

it has been reported earlier as bug to gcc,
see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9737

but it seems they don't want to fix it because it is likely to be valid
in std c++ in the future, DR 150 is mentioned in that context.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top