Template specialization for templated and primitive type template parameters

Discussion in 'C++' started by case2005, Feb 13, 2005.

  1. case2005

    case2005 Guest

    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
    case2005, Feb 13, 2005
    #1
    1. Advertising

  2. "case2005" <> 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" | \|__| |
    | | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|
    Nicolas Pavlidis, Feb 13, 2005
    #2
    1. Advertising

  3. case2005

    case2005 Guest

    Re: Template specialization for templated and primitive type tem

    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, Feb 13, 2005
    #3
  4. Re: Template specialization for templated and primitive type tem

    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
    Nicolas Pavlidis, Feb 13, 2005
    #4
    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. Replies:
    7
    Views:
    590
    Victor Bazarov
    May 9, 2005
  2. Joseph Turian
    Replies:
    2
    Views:
    455
  3. red floyd
    Replies:
    9
    Views:
    337
  4. David O
    Replies:
    2
    Views:
    415
    David O
    Jan 10, 2007
  5. Daniel Pitts
    Replies:
    7
    Views:
    462
Loading...

Share This Page