473,748 Members | 10,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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! ]

Oct 23 '08 #1
28 2562
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.
Oct 23 '08 #2
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:ja******* **@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
Oct 23 '08 #3
"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.
Oct 24 '08 #4
SG
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
Oct 24 '08 #5
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! ]

Oct 24 '08 #6
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! ]

Oct 24 '08 #7
HL
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! ]

Oct 24 '08 #8
Gil
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:
http://www.two-sdg.demon.co.uk/curbr...onversions.pdf
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! ]

Oct 24 '08 #9
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:ja******* **@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! ]

Oct 24 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2685
by: Gnolen | last post by:
Hi, I am really getting crazy here! I just do not get why this happens with the borders of the td/tr! I just want a border on the bottom of the rows(or td) but I just can't do it!!! I have tried so many different ways but I just thought this would do it: td {border-bottom: #000000 1px solid;} But no! What am I doing wrong? Because I can have a border of a td or tr in FF, right?! Thankful for any help here! Table and CSS is below..
3
383
by: INGSOC | last post by:
I spent a few hours today setting up remote debugging in VS.2003 I had to disable the firewall, subvert the security scheme and let anonymous users have full access to my COM+ objects on my Windows XP workstation, but other than that, it was smooth sailing. And, oh yeah. I also have to run remote desktop sharing terminal between my XP workstation and the W2K target or I can't RPC to the destination machine.
19
1710
by: Daniel Billingsley | last post by:
I think it's really nice to be able to do code like someVariable = someMethod(new SomeClass("some text")); However, as method signatures are number and types of parameters, you're limited to having one constructor that accepts a single string, for example. Call me crazy, but wouldn't it be useful to be able to have a syntax where you could set properties in a statement like the one above, without being limited to just what you could...
2
1018
by: rooster575 | last post by:
Im getting the following error on a page where I try to access an external web service. Every time I get the error the .dll name is different! Thanks for any help. ============================================================================ = System.Web.HttpUnhandledException: Exception of type System.Web.HttpUnhandledException was thrown. --->
11
3033
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. It works perfectly (assigning the correct images and the correct onclick events to the correct <atags):
2
3629
by: kheitmann | last post by:
OK, so I have a blog. I downloaded the "theme" from somewhere and have edited a few areas to suit my needs. There are different font themes within the page theme. Long story short, my "Text Posts" are supposed to be in the font: Georgia, but they are showing up in "Times New Roman"...blah! I can't find anything wrong in the code, but who am I trying to fool? I know nothing about this stuff. The code is below. The parts that I *think*...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.