# [C++]Why can't I inline this function?

This topic is 3675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I'm created a program that checks if a number is palindrome. palindrome.h
#ifndef PALINDROME_H
#define PALINDROME_H

#include <iostream>
#include <string>
#include <sstream>
#include <cmath>

using std::cout;	using std::cin;	using std::getline;
using std::string;	using std::stringstream;

int get_power(int num);
int get_int();
int palindrome(int num);
bool is_palindrome(int num);

#endif


palindrome.cpp
#include "palindrome.h"

int get_power(int num)
{
int power = 1;
while(num != 0)
{
num /= 10;
power *= 10;
}
power /= 10;
return power;
}

int get_int()
{
string str;
getline(cin, str);
stringstream ss(str);
int num;
ss >> num;
return num;
}

int palindrome(int num)
{
int the_palindrome = 0,
power = get_power(num),
temp = power;

for(int i = 1; i <= power; i *= 10, temp /= 10)
the_palindrome += num / i % 10 * temp;

return the_palindrome;
}

bool is_palindrome(int num)
{
return num == palindrome(num);
}


Take a look at bool is_palindrome. This way it works fine, but if I inline it, I get a linker error. What's wrong?

##### Share on other sites
Some linkers might be able to inline code too, though.

If you mean you want to move the implementation to the h file, so it can be inlined by the compiler, you might need the inline keyword. As far as I understand, this keyword has less to do with actual inlining (the compiler is free to take this only as a suggestion) and more with allowing "multiple definitions" of the same function, if the header is included from more than one source file.

##### Share on other sites
Quote:
 Original post by DevFredAs far as I know, you can only inline what is known to the compilation unit that calls it. The linker can't inline calls.

Wrong; there is an option (for the VC++ linker at least, and I suspect GCC's can do the same thing) which allows for link time code generation, which allows for inlining of function calls among other things.

##### Share on other sites
Quote:
 Original post by phantomWrong

I stand corrected then.

##### Share on other sites
#include means that contents of the file are planted to that point of the file. That means you are declaring a function which does not exists, and then creating an inline function that has the same name. Does not sound very correct way to do it, or does it?

Inline function has to be declared in the header file, if you want to use it somewhere. Just plant
inline bool is_palindrome(int num){	return num == palindrome(num);}
into the palindrome.h.

Other way to do it is planting the said function into beginning of the palindrome.cpp, if that is the only file you call it from, and removing the declaration that is in the header file.

##### Share on other sites
Beside, "reverse" would be a more appropriate name for your "palindrome" function.

##### Share on other sites
Looks to me like it's one of two things:

1.) Most likely -- simply a limitation of the compiler. Try to inline something simple like void test(){cout << "this is a test";}; If you get an error, you know it is simply a limitation of the compiler.

2.) If that doesn't work, then it is probably because you are calling a function that so far exists only as a function declaration. I can't see why it would be this, but this is another compiler dependent thing, so who knows. To test it, try to inline the palindrome function as well. If it works, then this was the problem. If not, I have no idea what the problem could be :(

##### Share on other sites
Quote:
 Original post by ville-v#include means that contents of the file are planted to that point of the file. That means you are declaring a function which does not exists, and then creating an inline function that has the same name. Does not sound very correct way to do it, or does it?Inline function has to be declared in the header file, if you want to use it somewhere. Just plant inline bool is_palindrome(int num){ return num == palindrome(num);} into the palindrome.h.Other way to do it is planting the said function into beginning of the palindrome.cpp, if that is the only file you call it from, and removing the declaration that is in the header file.

Just a quick note: VC is free to ignore the inline suggestion (at least using its default settings).

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013164
×