[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Crazy Local Class

cpluslearn

10/23/2008 3:58:00 AM

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/resource... for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

27 Answers

James Kanze

10/23/2008 8:01:00 PM

0

On Oct 23, 3:36 pm, "Fraser Ross" <z...@zzzzzz.com> wrote:
> 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.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Fraser Ross

10/24/2008 7:45:00 AM

0

"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.


SG

10/24/2008 7:59:00 AM

0

On 24 Okt., 09:45, "Fraser Ross" <z...@zzzzzz.com> wrote:
>> [...] 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

Maxim Yegorushkin

10/24/2008 10:32:00 PM

0

On Oct 23, 4:58 am, cplusle...@gmail.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/resource... for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

James Kanze

10/24/2008 10:32:00 PM

0

On Oct 23, 5:58 am, cplusle...@gmail.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.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resource... for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

HL

10/24/2008 10:32:00 PM

0

On Oct 23, 11:58 am, cplusle...@gmail.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>::Local and wrap<std::string>::Local.
But if it's a template really doesn't matter, does it?


--
[ See http://www.gotw.ca/resource... for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

wasti.redl

10/24/2008 10:32:00 PM

0

On Oct 23, 5:58 am, cplusle...@gmail.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/resource... for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Gil

10/24/2008 10:32:00 PM

0

On Oct 22, 11:58 pm, cplusle...@gmail.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:
CopyConstructibleConcept and (based on your example),
OutputStreamableConcept.

you can find some good info on discriminated types
(holders that can contain different types) here:
http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConve...

> 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/resource... for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Anthony Williams

10/24/2008 10:43:00 PM

0

cpluslearn@gmail.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.justsoftwaresolut...
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

[ See http://www.gotw.ca/resource... for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

James Kanze

10/25/2008 8:44:00 AM

0

On Oct 24, 9:45 am, "Fraser Ross" <z...@zzzzzz.com> wrote:
> "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.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34