473,626 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1769
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<f ile_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
2601
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 the code below. The function template < typename T >
7
4942
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
7
2471
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
7
2140
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 y = x; but I can't understand the last one which is Array<int>::element_type y = x; We have a typedef T element_type; in the template Array class see below
10
2088
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. ========================= template <class T> myClass {....} ;
12
2612
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 an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
2
2009
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
2
3292
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 that this violates the standard (14.3.1.2), and sadly does not work with g++ (3.4.3, did not try newer). I was essentialy able to run some code for each local type before main() - see the source code below.
2
2587
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 I'm doing something wrong and g++ just doesn't notice, or if the people at Intel are doing something weird... What am I trying to do? I'm trying to implement a template class that inherits from the Blitz++ array library #include <blitz/array.h>...
0
8202
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
8707
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...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8510
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
6125
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...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
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

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.