473,425 Members | 1,805 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,425 software developers and data experts.

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<stringtest1;
test1.f();
+++++++++++++++++++++

is "typeid(Value) == 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 2917
In article
<49*********************************************** ****@gmail.com>, David
Portabella <da**************@gmail.comwrote:
[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<stringtest1;
test1.f();

is "typeid(Value) == 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_false )
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********@gishpuppy.com (blargg) wrote:
In article
<49*********************************************** ****@gmail.com>, David
Portabella <da**************@gmail.comwrote:
>[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<stringtest1;
test1.f();

is "typeid(Value) == 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_false )
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_false )
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....@gishpuppy.com (blargg) wrote:
In article
<49BF4EC8-28AD-492C-BCD3-492C7E57BC16%david.portabe...@gmail.com>, David
Portabella <david.portabe...@gmail.comwrote:
[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<stringtest1;
test1.f();
is "typeid(Value) == 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_false )
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...@gmail.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 objektorientierter Datenverarbeitung
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
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); } ...
3
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...
19
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...
18
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 ? //:...
6
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),...
4
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...
1
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...
3
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...
7
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...
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
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,...
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
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...
0
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,...
0
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...
0
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...

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.