Unused functions

Hi, I have the following test program that I'm compiling with (g++ -O3 -Wall):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
class mytest{
  public:
  void print1();
  void print2();
  void print3();
  void print4();
};

void mytest::print1(){cout << "Hello 1\n";}
void mytest::print2(){cout << "Hello 2\n";}
void mytest::print3(){cout << "Hello 3\n";}
void mytest::print4(){cout << "Hello 4\n";}

int main ()
{
    mytest t;

    t.print1();
    t.print1();
    t.print1();
    t.print1();

  return 0;
}


If I replace the calls to the print?() functions with:

1
2
3
4
t.print1();
t.print2();
t.print3();
t.print4();


The executable size stays the same.

Shouldn't the compiler NOT include the code for the unused functions in the first case?

I was just wondering about this especially regarding the standard libraries like the string class, if all the find, convert, etc. functions are included in the binary even if they're not used....

Thanks.
In basic class all methods(and unused of course) is included to a binary file.

But if you use template class than only used methods will be compiled.

And so, you shouldn't be afraid of std::string, because it's typedef on template class
Last edited on
Topic archived. No new replies allowed.