template specialization, nested?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schüle Daniel

    #1

    template specialization, nested?

    Hello all,

    my question is basically wheather it's possible to
    have nested templates .. I mean the following
    if there are one templ. class and one global templ. function

    is it possible to specialize the function for say <int>
    but this for all possible classes (from class template)

    the code is here

    template <typename>
    class X;

    template <typename type>
    void foo(X<type> &);

    template <typename type = int>
    class X
    {
    friend void foo<type>();
    type x;
    public:
    X(type x) : x(x) {}
    };

    template <typename type>
    void foo(X<type> & x)
    {
    cout << "with type = " << typeid(type).na me() << endl;
    cout << x.x << endl;
    }

    template <> // this seems not ok
    template <typename type> // but i need "type" for X
    void foo<void>(X<typ e> & x)
    {
    cout << "this is void overload" << endl;
    cout << x.x << endl;
    }

    int main()
    {
    X<> x(1); // int
    foo<void>(x);
    return EXIT_SUCCESS;
    }

    Regards, Daniel

    g++ --version
    g++ (GCC) 4.0.2

  • Victor Bazarov

    #2
    Re: template specialization, nested?

    Schüle Daniel wrote:[color=blue]
    > my question is basically wheather it's possible to
    > have nested templates .. I mean the following
    > if there are one templ. class and one global templ. function
    >
    > is it possible to specialize the function for say <int>
    > but this for all possible classes (from class template)[/color]

    Uh... I am not sure I understand what you're trying to accomplish.
    [color=blue]
    >
    > the code is here
    >
    > template <typename>
    > class X;
    >
    > template <typename type>
    > void foo(X<type> &);
    >
    > template <typename type = int>
    > class X
    > {
    > friend void foo<type>();[/color]

    There is no 'foo' without arguments! Did you mean

    friend void foo<type>(X&);

    ?
    [color=blue]
    > type x;
    > public:
    > X(type x) : x(x) {}
    > };
    >
    > template <typename type>
    > void foo(X<type> & x)
    > {
    > cout << "with type = " << typeid(type).na me() << endl;
    > cout << x.x << endl;
    > }
    >
    > template <> // this seems not ok
    > template <typename type> // but i need "type" for X[/color]

    OK. So, you're trying to define a specialisation of 'foo' on 'void'
    so that the argument is now a template. IOW, you're defining another
    template here... That's not possible. Besides, there are no partial
    specialisations of function templates.

    Why do you think you need this construct?
    [color=blue]
    > void foo<void>(X<typ e> & x)
    > {
    > cout << "this is void overload" << endl;
    > cout << x.x << endl;
    > }
    >
    > int main()
    > {
    > X<> x(1); // int
    > foo<void>(x);
    > return EXIT_SUCCESS;
    > }
    >
    > Regards, Daniel
    >
    > g++ --version
    > g++ (GCC) 4.0.2
    >[/color]

    V
    --
    Please remove capital As from my address when replying by mail

    Comment

    • Schüle Daniel

      #3
      Re: template specialization, nested?

      Hello Victor,
      [color=blue][color=green]
      >> template <typename>
      >> class X;
      >>
      >> template <typename type>
      >> void foo(X<type> &);
      >>
      >> template <typename type = int>
      >> class X
      >> {
      >> friend void foo<type>();[/color]
      >
      >
      > There is no 'foo' without arguments! Did you mean
      >
      > friend void foo<type>(X&);[/color]

      yes, of course you are right
      [color=blue][color=green]
      >> type x;
      >> public:
      >> X(type x) : x(x) {}
      >> };
      >>
      >> template <typename type>
      >> void foo(X<type> & x)
      >> {
      >> cout << "with type = " << typeid(type).na me() << endl;
      >> cout << x.x << endl;
      >> }
      >>
      >> template <> // this seems not ok
      >> template <typename type> // but i need "type" for X[/color]
      >
      >
      > OK. So, you're trying to define a specialisation of 'foo' on 'void'
      > so that the argument is now a template. IOW, you're defining another
      > template here... That's not possible. Besides, there are no partial
      > specialisations of function templates.[/color]

      something like

      template <typename type>
      void foo() {}

      template <>
      void foo<int>(){}

      my understanding of it is, that this is fully specialized
      and is ok, my compiler at least doesn't complain
      [color=blue]
      > Why do you think you need this construct?[/color]
      I thought it's time to get familar with template usage
      (to write libraries)
      so mostly this an academic case
      [color=blue][color=green]
      >> void foo<void>(X<typ e> & x)[/color][/color]

      isn't it fully specialized?

      Regards, Daniel

      Comment

      • Victor Bazarov

        #4
        Re: template specialization, nested?

        Schüle Daniel wrote:[color=blue]
        > [..]
        > something like
        >
        > template <typename type>
        > void foo() {}
        >
        > template <>
        > void foo<int>(){}
        >
        > my understanding of it is, that this is fully specialized
        > and is ok, my compiler at least doesn't complain[/color]

        Yes. Fully specialised function templates are OK.
        [color=blue][color=green]
        >> Why do you think you need this construct?[/color]
        > I thought it's time to get familar with template usage
        > (to write libraries)
        > so mostly this an academic case
        >[color=green][color=darkred]
        >>> void foo<void>(X<typ e> & x)[/color][/color]
        >
        > isn't it fully specialized?[/color]

        How would it be if 'type' is unknown?

        V
        --
        Please remove capital As from my address when replying by mail


        Comment

        • Schüle Daniel

          #5
          Re: template specialization, nested?

          [..]
          [color=blue][color=green][color=darkred]
          >>>>void foo<void>(X<typ e> & x)[/color]
          >>
          >>isn't it fully specialized?[/color]
          >
          >
          > How would it be if 'type' is unknown?[/color]

          my minds eye saw only
          void foo<void>
          :)

          thanks for correction

          Comment

          Working...