473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

local type as template argument

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.

I know that to make it work, one could simply make the class non-local,
but I'd like to keep the classes local and achieve some pre-main
initialization for them. I know that the standard is clear about this,
but maybe there is some trick...? The reason for all this is my desire
to see all locally-defined strings (so I can do localization etc.)

Thanks for suggestions,
Filip
#include <iostream>
#include <list>
using namespace std;

// common interface
struct XC {
virtual const char* get_data() = 0;
virtual ~XC() {}
};
typedef list<XC*XCL;
XCL global_list;

template<typena me Y>
struct X : public XC {
X() {
// make this instance globally accessible...
// this runs before main() for each X
// instantiation
global_list.pus h_back(this);
}
virtual const char* get_data() {
return get_data_static ();
}
static X initializer;
// return data associated with Y
static const char* get_data_static () {
return data;
}
// data associated with Y
static const char* data;
};

template<typena me Y>
const char* X<Y>::data=Y::g etYData();

template<typena me YX<YX<Y>::initi alizer;

int main(int argc,char* argv[]) {
// the local type whose data
// will be visible globally
struct Y {
static const char* getYData() {
return "some data";
// make sure X<Y>::initializ er exists
X<Y>::initializ er;
}
};
// walk the list of X specializations :
XCL::iterator i, e=global_list.e nd();
for(i=global_li st.begin(); i!=e; ++i)
cout << (*i)->get_data() << endl;
// use X<Ydirectly:
cout << X<Y>::get_data_ static() << endl;
return 0;
}

May 21 '07 #1
2 3301
On 2007-05-21 08:30, Filip Konvicka wrote:
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.

I know that to make it work, one could simply make the class non-local,
but I'd like to keep the classes local and achieve some pre-main
initialization for them. I know that the standard is clear about this,
but maybe there is some trick...? The reason for all this is my desire
to see all locally-defined strings (so I can do localization etc.)
As you said the standard is quite clear on the matter (will hopefully
change in the next standard) so the only trick that I can come up with
is to search the gcc documentation that enables some non-standard
behaviour (just like MSVC8).

--
Erik Wikström
May 21 '07 #2
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.

I know that to make it work, one could simply make the class non-local,
but I'd like to keep the classes local and achieve some pre-main
initialization for them. I know that the standard is clear about this,
but maybe there is some trick...? The reason for all this is my desire
to see all locally-defined strings (so I can do localization etc.)

As you said the standard is quite clear on the matter (will hopefully
change in the next standard) so the only trick that I can come up with
is to search the gcc documentation that enables some non-standard
behaviour (just like MSVC8).
Thanks for your reply. gcc does not seem to have such feature (I
checked v.4.1.2).

Nevermind, I just wanted to make sure that I did not overlook
something obvious. I'll stick to non-local types then, and wait
eagerly for the next standard (I read some proposals about this, but
it seems to me that it will take years to get into the standard, if
ever).

Cheers,
Filip

May 22 '07 #3

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

Similar topics

2
4980
by: Gabriel Schreiber | last post by:
Hi this code: #include <vector> void f() { struct arg{}; std::vector<arg> v; };
10
7334
by: Matthias | last post by:
Hello, I thought one major advantage of using functors as e.g. sorting predicates over functions would be that I can do something like this: void foo() { class Predicate { public:
3
2732
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename Foo> class Bar { private: Foo foo;
4
1773
by: Neelesh Bodas | last post by:
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;
4
3641
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple inheritance (avoiding prosperation of virtual func tables). -- At the same time, a class taking N types shall contain a virtual member function that calls a function according to the number of arguments That means, something like:
17
1820
by: Steven T. Hatton | last post by:
I didn't think this was possible in C++. I don't believe Stroustrup mentions this anywhere in TC++PL(SE). I've never seen it used in code that I can recall. Is this feature widely supported? Is it used? #include <iostream> struct Bar { void f() { struct Local{Local(){std::cout <<"I am local"<<std::endl; }}; Local l; }
1
1994
by: slrj | last post by:
Can I use a local struct to store data in the "set" conatiner? If so, what is the correct method to do so? Compiler: Sun Studio 10 on Unix (Sun Solaris) #include<iostream> #include<set> using namespace std;
23
1836
by: deepakvsoni | last post by:
Why does C++ restrict definition of functions with in functions? int f1() { int f2() { ..... } } //This is not supported. Thanks in advance to anybody who replies..
28
2569
by: cpluslearn | last post by:
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:
0
9568
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
9404
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
10164
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
9959
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
9835
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...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
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();...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.