typeid resolved at compilation time?

  • Thread starter David Portabella
  • Start date
D

David Portabella

Hello,

I have the following template class:
++++++++++++++++++++++
template <class Value> class 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<string> test1;
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
 
D

David Portabella

David said:
[trimmed]
template <class Value> class Test {
public:
void f() {
if (typeid(Value) == typeid(string))
cout << "Value is a string" << endl;
else
cout << "Value is not a string" << endl;
}
};

Test<string> test1;
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
 
M

Marcel Müller

Hi!

David said:
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
 
P

Paul Carter

[email protected] (blargg) said:
David said:
[trimmed]
template <class Value> class Test {
public:
void f() {
if (typeid(Value) == typeid(string))
cout << "Value is a string" << endl;
else
cout << "Value is not a string" << endl;
}
};
Test<string> test1;
test1.f();
is "typeid(Value) == typeid(string)" resolved at compilation time or
at execution time?
Depends on the compiler.
3 == 3 could be resolved at run-time as well. Depends on the compiler.
// 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.
 
J

James Kanze

On Aug 7, 11:38 am, David Portabella <[email protected]>
wrote:

[...]
Is it ever relevant?
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top