473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

template instantiation with local type

Hello all,

Is there a way to pass an object of local type to a template?

template <class T> int fun(const T& x) { return 1; }

int bar()
{
enum Color { Red, Green, Blue};
Color c;
fun(c); // Error, Color is local to bar()
}

Dec 9 '05 #1
4 1760
Neelesh Bodas wrote:
Hello all,

Is there a way to pass an object of local type to a template?


No. If you want to use a type as template argument, that type must have
external linkage, and local types don't have any linkage.

Dec 9 '05 #2

Rolf Magnus wrote:
No. If you want to use a type as template argument, that type must have
external linkage, and local types don't have any linkage.


Great. Thanks.

Dec 9 '05 #3

Neelesh Bodas wrote:
Hello all,

Is there a way to pass an object of local type to a template?

template <class T> int fun(const T& x) { return 1; }

int bar()
{
enum Color { Red, Green, Blue};
Color c;
fun(c); // Error, Color is local to bar()
}


No. The usual workaround is to place the declarations in an anonymous
namepspace:

template <class T>
struct TemplateClass
{
};

namespace
{
enum Color { Red, Green, Blue};
Color c;

}

int main()
{
TemplateClass<file_scope_type> a;

//...
}

An anonymous namespace essentially allows local types at the file level
to be used as template type parameters.

Greg

Dec 10 '05 #4
Neelesh Bodas wrote:
Hello all,

Is there a way to pass an object of local type to a template?

template <class T> int fun(const T& x) { return 1; }

int bar()
{
enum Color { Red, Green, Blue};
Color c;
fun(c); // Error, Color is local to bar()
}


No. The usual suggestion is to place the declarations in an anonymous
namespace:

template <class T> int fun(const T& x) { return 1; }

namespace {
enum Color { Red, Green, Blue};
}

int main()
{
Color c;

fun(c); // OK, Color is unique to local file
}

An anonymous namespace essentially allows local types at the file level
to be used as template type parameters.

Greg

Dec 10 '05 #5

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

Similar topics

4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
7
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Filip Konvicka | last post by:
Hi all, I wonder whether anyone has seen a workaround for this somewhere, perhaps in Boost or elsewhere... I succeeded using a local type as template argument in MSVC 8.0, but I found out...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.