template template argument

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fei Liu

    #1

    template template argument

    #include <vector>
    #include <iostream>

    template <template <typename T, typename Allocclass C>
    struct A{
    //C<Tc;
    //A(){ c[0]=10; }
    void print(){ std::cout << "A" << std::endl; }
    };

    int main(){
    A<std::vectora ;
    a.print();
    }

    This posted code seems the only valid way of using template template
    argument in this context. I've tried a few combinations of rearranging
    things but they all failed the compiler. I am wondering what's the
    point of a A<std::vectora ? It really cannot contain anything, can it?

    Thanks,

  • Victor Bazarov

    #2
    Re: template template argument

    Fei Liu wrote:
    #include <vector>
    #include <iostream>
    >
    template <template <typename T, typename Allocclass C>
    struct A{
    //C<Tc;
    //A(){ c[0]=10; }
    void print(){ std::cout << "A" << std::endl; }
    };
    >
    int main(){
    A<std::vectora ;
    a.print();
    }
    >
    This posted code seems the only valid way of using template template
    argument in this context. I've tried a few combinations of rearranging
    things but they all failed the compiler.
    Not that it's necessary, but if you're wondering about the reasons, you
    should consider posting the failed variations and compiler diagnostics.
    Then somebody could explain...
    I am wondering what's the
    point of a A<std::vectora ? It really cannot contain anything, can
    it?
    Well, here it can't. But chang it to

    template<class T, template<class, classclass C...

    and uncomment the C<Tthing, make a couple of changes, and you might
    actually get a working wrapper around a standard container.

    I am guessing that what you have here is just an example of how to use
    template template argument. Nothing more, really. Probably isn't in
    any way intended to be useful.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Fei Liu

      #3
      Re: template template argument


      Victor Bazarov wrote:
      Fei Liu wrote:
      #include <vector>
      #include <iostream>

      template <template <typename T, typename Allocclass C>
      struct A{
      //C<Tc;
      //A(){ c[0]=10; }
      void print(){ std::cout << "A" << std::endl; }
      };

      int main(){
      A<std::vectora ;
      a.print();
      }

      This posted code seems the only valid way of using template template
      argument in this context. I've tried a few combinations of rearranging
      things but they all failed the compiler.
      >
      Not that it's necessary, but if you're wondering about the reasons, you
      should consider posting the failed variations and compiler diagnostics.
      Then somebody could explain...
      >
      I am wondering what's the
      point of a A<std::vectora ? It really cannot contain anything, can
      it?
      >
      Well, here it can't. But chang it to
      >
      template<class T, template<class, classclass C...
      >
      and uncomment the C<Tthing, make a couple of changes, and you might
      actually get a working wrapper around a standard container.
      >
      I am guessing that what you have here is just an example of how to use
      template template argument. Nothing more, really. Probably isn't in
      any way intended to be useful.
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask
      Thanks, with the hint, I am able to produce this snippet that works.
      But it does seem that it's going through too much trouble to achieve
      the result.

      #include <vector>
      #include <iostream>

      template <typename T, typename Alloc, template <typename, typename>
      class C>
      struct A{
      C<T, Allocc;
      A(){ c[0] = 10; }
      void print(){ std::cout << "A" << std::endl; }
      };

      int main(){
      A<int, std::allocator< int>, std::vector a;
      a.print();
      }

      Comment

      • Victor Bazarov

        #4
        Re: template template argument

        Fei Liu wrote:
        Victor Bazarov wrote:
        >Fei Liu wrote:
        >>#include <vector>
        >>#include <iostream>
        >>>
        >>template <template <typename T, typename Allocclass C>
        >>struct A{
        >> //C<Tc;
        >> //A(){ c[0]=10; }
        >> void print(){ std::cout << "A" << std::endl; }
        >>};
        >>>
        >>int main(){
        >> A<std::vectora ;
        >> a.print();
        >>}
        >>>
        >>This posted code seems the only valid way of using template template
        >>argument in this context. I've tried a few combinations of
        >>rearranging things but they all failed the compiler.
        >>
        >Not that it's necessary, but if you're wondering about the reasons,
        >you should consider posting the failed variations and compiler
        >diagnostics. Then somebody could explain...
        >>
        >>I am wondering what's the
        >>point of a A<std::vectora ? It really cannot contain anything, can
        >>it?
        >>
        >Well, here it can't. But chang it to
        >>
        > template<class T, template<class, classclass C...
        >>
        >and uncomment the C<Tthing, make a couple of changes, and you might
        >actually get a working wrapper around a standard container.
        >>
        >I am guessing that what you have here is just an example of how to
        >use template template argument. Nothing more, really. Probably
        >isn't in any way intended to be useful.
        >>
        >V
        >--
        >Please remove capital 'A's when replying by e-mail
        >I do not respond to top-posted replies, please don't ask
        >
        Thanks, with the hint, I am able to produce this snippet that works.
        But it does seem that it's going through too much trouble to achieve
        the result.
        >
        #include <vector>
        #include <iostream>
        >
        template <typename T, typename Alloc, template <typename, typename>
        class C>
        struct A{
        C<T, Allocc;
        A(){ c[0] = 10; }
        void print(){ std::cout << "A" << std::endl; }
        };
        >
        int main(){
        A<int, std::allocator< int>, std::vector a;
        a.print();
        }
        If you don't care about definition the standard allocator, you could
        use a "template typedef trick" (there is not template typedef yet, that
        is why I use the quotes):

        #include <vector>
        template<class Tstruct use_vector { typedef std::vector<Tty pe; };

        #include <list>
        template<class Tstruct use_list { typedef std::list<Ttype ; };

        #include <iostream>
        #include <typeinfo>

        template<class T, template<classc lass Cstruct A {
        typename C<T>::type c;
        A() { c.push_back(10) ; }
        void print() {
        std::cout << "A<" << typeid(T).name( )
        << ',' << typeid(c).name( ) << " >\n"; }
        };

        int main() {
        A<int, use_vectora;
        A<double, use_listd;
        a.print();
        d.print();
        }

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        • Fei Liu

          #5
          Re: template template argument


          Fei Liu wrote:
          Victor Bazarov wrote:
          Fei Liu wrote:
          #include <vector>
          #include <iostream>
          >
          template <template <typename T, typename Allocclass C>
          struct A{
          //C<Tc;
          //A(){ c[0]=10; }
          void print(){ std::cout << "A" << std::endl; }
          };
          >
          int main(){
          A<std::vectora ;
          a.print();
          }
          >
          This posted code seems the only valid way of using template template
          argument in this context. I've tried a few combinations of rearranging
          things but they all failed the compiler.
          Not that it's necessary, but if you're wondering about the reasons, you
          should consider posting the failed variations and compiler diagnostics.
          Then somebody could explain...
          I am wondering what's the
          point of a A<std::vectora ? It really cannot contain anything, can
          it?
          Well, here it can't. But chang it to

          template<class T, template<class, classclass C...

          and uncomment the C<Tthing, make a couple of changes, and you might
          actually get a working wrapper around a standard container.

          I am guessing that what you have here is just an example of how to use
          template template argument. Nothing more, really. Probably isn't in
          any way intended to be useful.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask
          >
          Thanks, with the hint, I am able to produce this snippet that works.
          But it does seem that it's going through too much trouble to achieve
          the result.
          >
          #include <vector>
          #include <iostream>
          >
          template <typename T, typename Alloc, template <typename, typename>
          class C>
          struct A{
          C<T, Allocc;
          A(){ c[0] = 10; }
          void print(){ std::cout << "A" << std::endl; }
          };
          >
          int main(){
          A<int, std::allocator< int>, std::vector a;
          a.print();
          }
          The updated version is even better to illustrate the point of template
          template argument, to coordinate template arguments so that one does
          not write
          A<int, std::allocator< double>, std::vectora and get confused with
          compiler errors. Well to be honest it could be very confusing anyway.

          #include <vector>
          #include <iostream>

          template <typename T, template <typenameclas s Alloc, template
          <typename, typenameclass C>
          struct A{
          C<T, Alloc<T c;
          A(){ c[0] = 10; }
          void print(){ std::cout << "A" << std::endl; }
          };

          int main(){
          A<int, std::allocator, std::vector a;
          a.print();
          }

          Comment

          Working...