Crazy Local Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cpluslearn@gmail.com

    Crazy Local Class

    Hi,
    I have a local class inside a function template. I am able to wrap
    any type in my local class. Is it legal C++? What type of class is
    Local? Is it a class template or regular class?
    Thanks in advance.
    --dhina
    ---------------------------------------------------------------------------------------------------------------------------------------------
    class Foo
    {
    public:
    virtual ~Foo() {}
    virtual void print() = 0;
    };

    template< typename T >
    Foo* wrap(const T& t)
    {
    class Local : public Foo
    {
    public:
    Local(const T& t):val_(t) { }
    void print()
    {
    cout << "In Local::print()" << val_ << endl;
    }
    private:
    T val_;
    };
    return new Local(t);
    }

    int main(void)
    {
    int x = 50;
    Foo* ptr = wrap(x);
    ptr->print();
    std::string s("wrap");
    ptr = wrap(s);
    ptr->print();
    }

    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.m oderated. First time posters: Do this! ]

  • Fraser Ross

    #2
    Re: Crazy Local Class

    Local is a non-template class. A local class can improve locality of
    symbols. The type of the class can be used as a template argument and
    so it is not entirely hidden because its local to a function.

    Fraser.


    Comment

    • James Kanze

      #3
      Re: Crazy Local Class

      On Oct 23, 3:36 pm, "Fraser Ross" <z...@zzzzzz.co mwrote:
      Local is a non-template class.  A local class can improve
      locality of symbols.  The type of the class can be used as a
      template argument and so it is not entirely hidden because its
      local to a function.
      Not sure I understand what you are trying to say, but a local
      class cannot be used as a template argument.

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      • Fraser Ross

        #4
        Re: Crazy Local Class

        "James Kanze"
        >Not sure I understand what you are trying to say, but a local
        >class cannot be used as a template argument.
        Its in the draft at 14.3.1/2.

        Fraser.


        Comment

        • SG

          #5
          Re: Crazy Local Class

          On 24 Okt., 09:45, "Fraser Ross" <z...@zzzzzz.co mwrote:
          >[...] but a local class cannot be used as a template argument.
          Its in the draft at 14.3.1/2.
          For C++0x, yes. But it doesn't work now.

          Cheers,
          SG

          Comment

          • Maxim Yegorushkin

            #6
            Re: Crazy Local Class

            On Oct 23, 4:58 am, cplusle...@gmai l.com wrote:
            I have a local class inside a function template. I am able to wrap
            any type in my local class.
            It is a common C++ idiom sometimes called type erasure.
            Is it legal C++?
            It is legal.
            What type of class is Local?
            The one declared in a function scope.
            Is it a class template or regular class?
            Class template is not a class in a general sense, until you
            instantiate it. Once a class template is instantiated it yields a
            class. Thus, local class is a class.

            --
            Max



            --
            [ See http://www.gotw.ca/resources/clcm.htm for info about ]
            [ comp.lang.c++.m oderated. First time posters: Do this! ]

            Comment

            • wasti.redl@gmx.net

              #7
              Re: Crazy Local Class

              On Oct 23, 5:58 am, cplusle...@gmai l.com wrote:
              Hi,
              I have a local class inside a function template. I am able to wrap
              any type in my local class. Is it legal C++? What type of class is
              Local? Is it a class template or regular class?
              Like members of a class template, be they data members, functions, or
              inner types, local classes in template functions are template
              "members", meaning that there is one version per instantiation of the
              outer template.

              Sebastian


              --
              [ See http://www.gotw.ca/resources/clcm.htm for info about ]
              [ comp.lang.c++.m oderated. First time posters: Do this! ]

              Comment

              • HL

                #8
                Re: Crazy Local Class

                On Oct 23, 11:58 am, cplusle...@gmai l.com wrote:
                Hi,
                I have a local class inside a function template. I am able to wrap
                any type in my local class. Is it legal C++? What type of class is
                Local? Is it a class template or regular class?
                Thanks in advance.
                --dhina
                This is absolutedly legal C++. As an answer, local class refer to all
                classes are not defined in global, namespace scopes. In your codes,
                Foo is regular class. However, I am not sure if Local is a template.
                Just like we don't usually call a member in a template class template
                method, I don't call a class in a template method template class. Even
                when deducting, there will be 2 Locals; Local with int and Local with
                string, but they are different types and both in local scope. Maybe I
                can use this term: wrap<int>::Loca l and wrap<std::strin g>::Local.
                But if it's a template really doesn't matter, does it?


                --
                [ See http://www.gotw.ca/resources/clcm.htm for info about ]
                [ comp.lang.c++.m oderated. First time posters: Do this! ]

                Comment

                • Gil

                  #9
                  Re: Crazy Local Class

                  On Oct 22, 11:58 pm, cplusle...@gmai l.com wrote:
                  Hi,
                  I have a local class inside a function template. I am able to wrap
                  any type in my local class.
                  you're not able to wrap _any_ type just types that model:
                  CopyConstructib leConcept and (based on your example),
                  OutputStreamabl eConcept.

                  you can find some good info on discriminated types
                  (holders that can contain different types) here:

                  Is it legal C++?
                  the exercised idiom (the local class itself) is perfectly legal
                  and imo interesting C++ code.
                  what makes it different than a placeholder approach like boost::any
                  is the simple memory management left entirely to user as opposed to
                  RAII exercised by boost::any.
                  judging by your example some form of RAII would have been better:)

                  the example has small(?) imperfections though:
                  missing delete ptr and missing return in main,
                  non-const pure virtual print in Foo etc.
                  What type of class is
                  Local? Is it a class template or regular class?
                  it's a 'regular' local class with no linkage but visible to RTTI
                  mechanism.
                  it models also an Adaptor to an already deduced type (that's the
                  interesting part).
                  it makes use of some sort of external polymorphism idiom by deriving
                  from a common interface for non related types.

                  beside boost::any you can also check boost::variant and Qt's QVariant
                  if you want to see some different implementations of discriminated
                  types.

                  cheers,
                  gill


                  --
                  [ See http://www.gotw.ca/resources/clcm.htm for info about ]
                  [ comp.lang.c++.m oderated. First time posters: Do this! ]

                  Comment

                  • James Kanze

                    #10
                    Re: Crazy Local Class

                    On Oct 23, 5:58 am, cplusle...@gmai l.com wrote:
                    I have a local class inside a function template. I am able to
                    wrap any type in my local class. Is it legal C++?
                    Sure.
                    What type of class is Local? Is it a class template or
                    regular class?
                    You can't define a template in local scope, period. You can
                    define a class, with two restrictions: all member functions must
                    be defined within the class definition, and the class may have
                    no static data members. Other than that, it's exactly like any
                    other class (except, obviously, for the name binding of the
                    class name.
                    class Foo
                    {
                    public:
                    virtual ~Foo() {}
                    virtual void print() = 0;
                    };
                    template< typename T >
                    Foo* wrap(const T& t)
                    {
                    class Local : public Foo
                    Here, Local is NOT a template. But you have a distinct Local
                    (defined differently) for each instantiation of wrap, which
                    makes it act like a template in some ways.

                    --
                    James Kanze (GABI Software) email:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


                    --
                    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
                    [ comp.lang.c++.m oderated. First time posters: Do this! ]

                    Comment

                    • Anthony Williams

                      #11
                      Re: Crazy Local Class

                      cpluslearn@gmai l.com writes:
                      I have a local class inside a function template. I am able to wrap
                      any type in my local class. Is it legal C++? What type of class is
                      Local? Is it a class template or regular class?
                      Yes, this is legal C++. Local is a normal class, but there is a
                      separate type for each instantiation of wrap<>.
                      template< typename T >
                      Foo* wrap(const T& t)
                      {
                      class Local : public Foo
                      {
                      public:
                      Local(const T& t):val_(t) { }
                      void print()
                      {
                      cout << "In Local::print()" << val_ << endl;
                      }
                      private:
                      T val_;
                      };
                      return new Local(t);
                      }
                      Anthony
                      --
                      Anthony Williams | Just Software Solutions Ltd
                      Custom Software Development | http://www.justsoftwaresolutions.co.uk
                      Registered in England, Company Number 5478976.
                      Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

                      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
                      [ comp.lang.c++.m oderated. First time posters: Do this! ]

                      Comment

                      • James Kanze

                        #12
                        Re: Crazy Local Class

                        On Oct 24, 9:45 am, "Fraser Ross" <z...@zzzzzz.co mwrote:
                        "James Kanze"
                        Not sure I understand what you are trying to say, but a local
                        class cannot be used as a template argument.
                        Its in the draft at 14.3.1/2.
                        I had heard that there had been a proposal to allow it, but I
                        was too lazy to look-up whether it had been adopted. But it's
                        still illegal in current C++ (C++03).

                        --
                        James Kanze (GABI Software) email:james.kan ze@gmail.com
                        Conseils en informatique orientée objet/
                        Beratung in objektorientier ter Datenverarbeitu ng
                        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                        Comment

                        • Fraser Ross

                          #13
                          Re: Crazy Local Class

                          Any non external entity can't be a template argument at present.

                          I want to use a string literal as an argument for a non-type parameter
                          but that can't be done. Is that a sensible restriction?

                          Fraser.


                          Comment

                          • Fraser Ross

                            #14
                            Re: Crazy Local Class

                            In the standards example below neither constructor is accessible and
                            only one is used.



                            template<class T, char* pclass X {
                            X();
                            X(const char* q) { }
                            };
                            X<int, "Studebaker "x1; // error: string literal as template-argument
                            char p[] = "Vivisectionist ";
                            X<int,px2; // OK



                            Fraser.


                            Comment

                            • Pete Becker

                              #15
                              Re: Crazy Local Class

                              On 2008-10-25 12:08:41 -0400, "Fraser Ross" <z@zzzzzz.comsa id:
                              >
                              I want to use a string literal as an argument for a non-type parameter
                              but that can't be done. Is that a sensible restriction?
                              >
                              Yes. Two string literals with the same sequence of characters can be at
                              the same address or at different addresses. So:

                              template <const char *class C
                              {
                              };

                              C<"asdf"c0;
                              C<"asdf"c1 = c0;

                              c0 and c1 could be the same type or they could be different types, so
                              in some cases the initialization of c1 would fail, and in others it
                              would succeed.

                              The way to do this is:

                              const char *param = "asdf";

                              C<paramc0;
                              C<paramc1 = c0; // OK

                              --
                              Pete
                              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
                              Standard C++ Library Extensions: a Tutorial and Reference
                              (www.petebecker.com/tr1book)

                              Comment

                              Working...