473,549 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typeid resolved at compilation time?

Hello,

I have the following template class:
+++++++++++++++ +++++++
template <class Valueclass Test {
public:
void f() {
if (typeid(Value) == typeid(string))
cout << "Value is a string" << endl;
else
cout << "Value is not a string" << endl;
}
};
+++++++++++++++ ++++++
Then, given the following code:
+++++++++++++++ ++++++
Test<stringtest 1;
test1.f();
+++++++++++++++ ++++++

is "typeid(Val ue) == typeid(string)" resolved at compilation time or
at execution time?

(that is, in the same way that "3==3" is resolved at compilation time,
because of gcc optimizes the code)

-------
And in general,
how to check if the compiler optimized such things?
that is, with g++ -S I get the assembler code, but it is difficult to
see something there. is there an intermediate language between cpp and
the -S output?
maybe something that converts c++ to c? that would be useful to see
what the compiler achieve to optimize.
Many thanks,
DAvid
Aug 7 '08 #1
5 2928
In article
<49************ *************** *************** *********@gmail .com>, David
Portabella <da************ **@gmail.comwro te:
[trimmed]
template <class Valueclass Test {
public:
void f() {
if (typeid(Value) == typeid(string))
cout << "Value is a string" << endl;
else
cout << "Value is not a string" << endl;
}
};

Test<stringtest 1;
test1.f();

is "typeid(Val ue) == typeid(string)" resolved at compilation time or
at execution time?
Depends on the compiler.
(that is, in the same way that "3==3" is resolved at compilation time,
because of gcc optimizes the code)
3 == 3 could be resolved at run-time as well. Depends on the compiler.
And in general,
how to check if the compiler optimized such things?
that is, with g++ -S I get the assembler code, but it is difficult to
see something there. is there an intermediate language between cpp and
the -S output?
// test.cpp

void at_run_time(); // not defined anywhere

int main()
{
if ( condition_that_ you_know_is_fal se )
at_run_time();
}

If the above program links, then condition was resolved at compile-time.
If you get a linker error about missing at_run_time(), then it wasn't
resolved at compile-time.
Aug 7 '08 #2
bl********@gish puppy.com (blargg) wrote:
In article
<49************ *************** *************** *********@gmail .com>, David
Portabella <da************ **@gmail.comwro te:
>[trimmed]
template <class Valueclass Test {
public:
void f() {
if (typeid(Value) == typeid(string))
cout << "Value is a string" << endl;
else
cout << "Value is not a string" << endl;
}
};

Test<stringtes t1;
test1.f();

is "typeid(Val ue) == typeid(string)" resolved at compilation time or
at execution time?

Depends on the compiler.
>(that is, in the same way that "3==3" is resolved at compilation time,
because of gcc optimizes the code)

3 == 3 could be resolved at run-time as well. Depends on the compiler.
>And in general,
how to check if the compiler optimized such things?
that is, with g++ -S I get the assembler code, but it is difficult to
see something there. is there an intermediate language between cpp and
the -S output?

// test.cpp

void at_run_time(); // not defined anywhere

int main()
{
if ( condition_that_ you_know_is_fal se )
at_run_time();
}

If the above program links, then condition was resolved at compile-time.
If you get a linker error about missing at_run_time(), then it wasn't
resolved at compile-time.
Hey, that's a good trick! :)

One more question in this direction,
how to test if the compiler toke into account or ignored the fact that a
function is declared inline?

+++++++++++++
inline int sum(int a, int b) {
return a+b;
}

int main() {
int c = sum(3,4);
}
+++++++++++++
Many thanks,
DAvid

Aug 7 '08 #3
Hi!

David Portabella schrieb:
> // test.cpp

void at_run_time(); // not defined anywhere

int main()
{
if ( condition_that_ you_know_is_fal se )
at_run_time();
}

If the above program links, then condition was resolved at compile-time.
If you get a linker error about missing at_run_time(), then it wasn't
resolved at compile-time.

Hey, that's a good trick! :)
Unfortunately it is not working in all ceses, because the compiler may
not discard unreachable code.
One more question in this direction,
how to test if the compiler toke into account or ignored the fact that a
function is declared inline?

+++++++++++++
inline int sum(int a, int b) {
return a+b;
}

int main() {
int c = sum(3,4);
}
Call the function some million times and call the same function from
another object module same as often and look at the runtime difference.
Marcel
Aug 7 '08 #4
On Aug 7, 11:38 am, David Portabella <david.portabe. ..@gmail.com>
wrote:
blargg....@gish puppy.com (blargg) wrote:
In article
<49BF4EC8-28AD-492C-BCD3-492C7E57BC16%da vid.portabe...@ gmail.com>, David
Portabella <david.portabe. ..@gmail.comwro te:
[trimmed]
template <class Valueclass Test {
public:
void f() {
if (typeid(Value) == typeid(string))
cout << "Value is a string" << endl;
else
cout << "Value is not a string" << endl;
}
};
Test<stringtest 1;
test1.f();
is "typeid(Val ue) == typeid(string)" resolved at compilation time or
at execution time?
Depends on the compiler.
(that is, in the same way that "3==3" is resolved at compilation time,
because of gcc optimizes the code)
3 == 3 could be resolved at run-time as well. Depends on the compiler.
And in general,
how to check if the compiler optimized such things?
that is, with g++ -S I get the assembler code, but it is difficult to
see something there. is there an intermediate language between cpp and
the -S output?
// test.cpp
void at_run_time(); // not defined anywhere
int main()
{
if ( condition_that_ you_know_is_fal se )
at_run_time();
}
If the above program links, then condition was resolved at compile-time.
If you get a linker error about missing at_run_time(), then it wasn't
resolved at compile-time.

Hey, that's a good trick! :)

One more question in this direction,
how to test if the compiler toke into account or ignored the fact that a
function is declared inline?

+++++++++++++
inline int sum(int a, int b) {
return a+b;

}

int main() {
int c = sum(3,4);}

+++++++++++++

Many thanks,
DAvid
The most direct way would be have the compiler create an assembly
listing for the code (-S for *nix compilers like g++) and look to see
if a subroutine call is made or not.

--
Paul
Aug 7 '08 #5
On Aug 7, 8:34 pm, Paul Carter <pacman...@gmai l.comwrote:
On Aug 7, 11:38 am, David Portabella <david.portabe. ..@gmail.com>
wrote:
[...]
One more question in this direction, how to test if the
compiler toke into account or ignored the fact that a
function is declared inline?
Is it ever relevant?
+++++++++++++
inline int sum(int a, int b) {
return a+b;
}
int main() {
int c = sum(3,4);}
+++++++++++++
The most direct way would be have the compiler create an
assembly listing for the code (-S for *nix compilers like g++)
and look to see if a subroutine call is made or not.
The compiler could generate an out of line instance, and still
generate the code inline in main. (FWIW: g++ more or less
ignores the "inline" in the above code. If optimization is
turned off, it calls the out of line version, and if
optimization is turned on, it will generate the code inline,
with or without the "inline" keyword". I expect that g++ is not
alone in this.)

--
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
Aug 8 '08 #6

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

Similar topics

11
3298
by: Jamie Burns | last post by:
Hello, I just did a simple benchmark: for (xx=0;xx<100000;xx++) { rDerived* derived = dynamic_cast<rDerived*>(object); if (derived) derived->setValue(message.data.messageSetInt.value); } against:
3
3730
by: Mike | last post by:
I want to use typeid() in a base class function to determine the name of the derived class. typeid(this) returns the name of the base class (which is an abstract class) rather than the derived class. I'd rather not require the typeid call in every derived class implementation. How do I get around this? I'm depending on the statement in...
19
3599
by: Marco Jez | last post by:
Hi everyone! I would like to use the reference returned by typeid as key in a std::map. Is it safe to assume that typeid(T) (where T is a type name) will always return the same reference to the same type_info structure for a given T? My map would look like this: typedef std::map<const std::type_info &, ......> Type_map; Cheers,
18
3638
by: Adam Zimny | last post by:
This is fragment of code from Bruce Eckel's Thinking in c++ ( last 3 couts are mine to show what happened ). The question is: is Bruce Eckel wrong or g++ ( my version is 3.2.3 ) is buggy ? //: C15:StaticHierarchyNavigation.cpp // Navigating class hierarchies with static_cast #include <iostream> #include <typeinfo> using namespace std;
6
2089
by: ma740988 | last post by:
I was trying to garner a feel for typeid and it's use with polymorphic types #include <iostream> template <class T> bool is_polymorphic() { bool result(false); typeid( (result=true), *(T*)0); return result;
4
1357
by: ezelasky | last post by:
I am programming in microsoft VC++ 7.1 and get an unhandled exception when I use typeid on a deferenced pointer. So I tried the example below, from the msdn site and I am seeing the same error as with my code, "Unhandled exception ... __non_rtti_object__". Perhaps I am missing a setting or something in my environment? Thanks,...
1
2106
by: moleskyca1 | last post by:
If I use RTTI, will every class have a typeid info? I am confused. When does compiler generate this info? Is it at run-time or at compile time? What if I have a template class, or the STL classes, will they also have typeid info?
3
17099
by: amanjsingh | last post by:
Hi, I am trying to implement Java Web Service using Apache Axis2 and Eclipse as a tool. I have created the basic code and deployed the service using various eclipse plugin but when I try to invoke the service using client stub, I get this error... Exception in thread "main" java.lang.Error: Unresolved compilation problems: org.apache cannot...
7
5584
by: Deepak Jharodia | last post by:
I'm using a templatized class in GCC based environ template<class A, class B> class foo {... ....} F; Now I want to know that particular instance of this class was instantiated with what template arguments. typeid.name() returns a strange string with all the info but abbreviated and probably platform specific.
0
7459
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...
0
7726
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. ...
1
7485
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...
0
7819
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...
1
5377
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...
0
5097
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...
0
3505
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...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.