Can anyone help with the following, I don't know if it's possible, but I'm
certain there must be a standard way of dealing with this.
I have the following:
template<typename FooBar, typename Foo>
class Bar {
private:
Foo foo;
}
Now I want to be able to handle when 'Foo' is a primitive type, or more
specifically a primitive pointer type, like 'char *' and also when Foo is
itself a template class:
template<typename FooBar, template<T> class Foo>
class Bar {
private:
Foo<FooBar> foo;
}
All the member functions and other fields of Bar are common to both
template classes. Can I do this with template specialization, if so how, I
don't quite get it.
I also tried to achieve this with the following, which isn't ideal,
because the client of the template must be aware that primitive types
must be wrapped in 'PrimitiveType':
template<class T>
struct PrimitiveType
{
typedef T Type;
};
template<class T>
class WrappedClass
{
public:
typedef WrappedClass<T> Type;
private:
};
template<class FooBar, template<class T> class Foo>
class Bar {
public:
Foo<FooBar>::Type wibble;
};
In theory, 'Bar' can have it's template parameter 'Foo' specified as
either 'PrimitiveType' or 'WrappedClass'.
Alas compilation with g++ simply gives me:
error: syntax error before `;' token
for the line Foo<FooBar>::Type wibble;
I greatly appreciate any light shed on either of these issues.
Many thanks 3 2622
"case2005" <ca******@private.email.com> writes: Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this.
I have the following:
template<typename FooBar, typename Foo> class Bar {
private: Foo foo; }
Now I want to be able to handle when 'Foo' is a primitive type, or more specifically a primitive pointer type, like 'char *' and also when Foo is itself a template class:
To find out if it is a pointer type you can do the following:
template< typename FooBar>
class Bar<FooBar, Foo*>
{
//......
};
If foo is some pointertype hte copiler will instanciate this
specialiization.
template<typename FooBar, template<T> class Foo> class Bar {
private: Foo<FooBar> foo; }
All the member functions and other fields of Bar are common to both template classes. Can I do this with template specialization, if so how, I don't quite get it.
Maybe I got you wrong, but I think that's not necessary, because the
template will be instaciated with a concrete type, build by a template,
such as:
Bar<int, vector<double> >
for example. SO there is no such specialization necessary, IMHO.
I also tried to achieve this with the following, which isn't ideal, because the client of the template must be aware that primitive types must be wrapped in 'PrimitiveType':
template<class T> struct PrimitiveType { typedef T Type; };
template<class T> class WrappedClass { public: typedef WrappedClass<T> Type;
private: };
template<class FooBar, template<class T> class Foo> class Bar { public:
Foo<FooBar>::Type wibble; };
Here is
typename Foo<FooBar>::Type wibble
required, becasue Type is a dependend name of the template Foo<...>.
I greatly appreciate any light shed on either of these issues.
I think what you are searchig for are so called typetraits: http://www.boost.org/libs/type_traits/index.html
This library work with a lot of differnet compilers.
If you use it you can ask the compiler if a special type is a primitive
type, or not and so on, and you'd have not to write a lot of template
specializations for each type.
HTH && Kind regards,
Nicolas
--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Nicolas
Thanks for this: typename Foo<FooBar>::Type wibble
That worked. However, I don't understand:
To find out if it is a pointer type you can do the following:
template< typename FooBar> class Bar<FooBar, Foo*> { //...... };
What is Foo here? I get a parse error:
error: parse error before `>' token for:
class Bar<FooBar, Foo*>
Even if this does work, it's doen't give me the polymorphism I'm looking
for in the second template argument for a primitive pointer type template
argument or a templated class as the template argument.
Maybe I got you wrong, but I think that's not necessary, because the template will be instaciated with a concrete type, build by a template, such as:
Bar<int, vector<double> >
for example. SO there is no such specialization > necessary, IMHO.
No, 'Bar' accepts 'Foo' without T specified, notice that T is passed by
field 'foo' as 'FooBar'
So for example I might pass 'WrappedClass':
to
template<typename FooBar, template<T> class Foo>
class Bar {
private:
Foo<FooBar> foo;
}
by
Bar<char, WrappedClass> bar;
bar.foo is then of type 'WrappedClass<char>' yes?
I think what you've missed is that I want 'Bar' to
accept both a primitive pointer like 'char *' in it's second template
argument and also a templated class template argument. Do you follow?
Kind regards
case2005 wrote: Nicolas
Thanks for this:
typename Foo<FooBar>::Type wibble
That worked. However, I don't understand:
To find out if it is a pointer type you can do the following:
template< typename FooBar> class Bar<FooBar, Foo*> { //...... };
What is Foo here? I get a parse error:
Sory, I should look like this:
template<typename FooBar, typename Foo>
class Bar<FooBar, Foo*>
{
//.....
};
This should compile.
error: parse error before `>' token for:
class Bar<FooBar, Foo*>
Even if this does work, it's doen't give me the polymorphism I'm looking for in the second template argument for a primitive pointer type template argument or a templated class as the template argument.
So for example I might pass 'WrappedClass':
to
template<typename FooBar, template<T> class Foo> class Bar {
private: Foo<FooBar> foo; }
by
Bar<char, WrappedClass> bar; bar.foo is then of type 'WrappedClass<char>' yes?
Ok.
I think what you've missed is that I want 'Bar' to accept both a primitive pointer like 'char *' in it's second template argument and also a templated class template argument. Do you follow?
Yes. I'm not sure, but I think that this will not work, and you'd have
to pass the hole type such as PrimitiveTypeWrapper<SomeType> to the
template, I don;t really think that specializations to template template
arguments are working.
To solve yout Problem with the pointers to primitive types:
You can specializie to each type, such as int* double* and so on, or use
the type traits, to find out the charactaristics of the type, and
produce code accroding to the results.
HTH
Nicolas This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul MG |
last post by:
Hi
Template partial specialization always seems like a fairly
straightforward concept - until I try to do it :).
I am trying to implement the...
|
by: Patrick Kowalzick |
last post by:
Dear all,
I have a question about default template parameters.
I want to have a second template parameter which as a default parameter,
but...
|
by: Nikolai Borissov |
last post by:
Is it possible to specialize a templated class defined within another
templeted class, like below:
template<typename U>
struct A
{...
|
by: Scott Frazer |
last post by:
I'm trying to do some template specialization and can't get it to work
quite right. I have a templated base class:
template <typename T> class...
|
by: mrstephengross |
last post by:
Ok, I've got a templated class Foo:
template<typename Tclass Foo { ; };
I want to specialize Foo for vectors holding any type:
// Something...
|
by: toton |
last post by:
Hi,
I want to specialize template member function of a template class .
It is creating some syntax problem ....
Can anyone say how to do it ?
...
|
by: stefan.bruckner |
last post by:
Hi,
I am looking for a way to achieve the following. I've tried a couple
of things, but they all ended up being too complicated:
I have a...
|
by: flopbucket |
last post by:
Hi,
I want to provide a specialization of a class for any type T that is a
std::map.
template<typename T>
class Foo
{
// ...
};
|
by: Ioannis Gyftos |
last post by:
Hello,
First the code :)
///////////////////////////////////////////////////////////////////////////////////
// in another header file
...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |