Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[C++] Must a member function of a templated class be inline?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, let me get this straight: Whenever a class, a member function or both are templated, it must be defined in the header file. Also, a member function to be inline must be declared inside the class definition, or separately, also in the header file, using the inline keyword. Now, I have a templated class with (sometimes templated) member functions, most of which I'd rather keep as a normal function, not an inline function. To keep the class definition clean, I declare the member function (in the header file) right after the class definition. However, when compiling the library and linking to it, I get a lot of "multiple definition of" errors. This is not the case when the member functions are defined inside the class definition. Why does inline-ness of a function determines the number of times it's declared? Must the functions be inline to fix this problem? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Can you post your header file? The way you're doing it sounds correct to me.


Quote:
"I declare the member function (in the header file) right after the class definition."


Do you mean DEFINE? Member functions must be declared in the class, but you may DEFINE them after the class definition.



// header Bla.h
#ifndef Bla_h
#define Bla_h

template < typename T >
class Bla
{
public:
void myFunc();
};

template < typename T >
void Bla< T >::myFunc()
{
// do stuff here
}

#endif


Share this post


Link to post
Share on other sites
Any function in a header should be either static or inline.

Member functions defined within class declarations are already inline.
For others, you need to specify inline yourself.

Share this post


Link to post
Share on other sites
Quote:

Whenever a class, a member function or both are templated, it must be defined in the header file.

Actually, they only need to be defined somewhere where they will be visible to anything that will consume it. In practice, however, this does mean that the overwhelming majority of template functions must be implemented in headers, otherwise they're available only to the translation unit of the .cpp file they're contained in (sometimes this is desirable). The compiler will not stop you from defining templates in a source file.

Quote:

Also, a member function to be inline must be declared inside the class definition

Close -- a member function defined inside the class definition is implicitly marked inline.

Quote:

Now, I have a templated class with (sometimes templated) member functions, most of which I'd rather keep as a normal function, not an inline function. To keep the class definition clean, I declare the member function (in the header file) right after the class definition.

Your terminology is inaccurate. I have an idea of what you are probably doing wrong, but you should provide an example of your code to be sure.

To declare a function is:

ReturnType FunctionnName(/* possible parameters */);


To define one is:

ReturnType FunctionnName(/* possible parameters */) {
// Code and stuff...
}


That said...
Quote:

Why does inline-ness of a function determines the number of times it's declared? Must the functions be inline to fix this problem?

It doesn't, and the number of times the function is declared isn't a problem. It's the number of times it's defined, which is what I suspect you're actually doing when you say you declare the functions outside the class definition.

If those functions are not marked inline, and are not templates (either directly or via their containing class), then they will be defined every time the compiler sees them, and since every source file is compiled in isolation, every source file that sees that header will define those functions, which means when the linker gets around to patching things up, it sees N different definitions for the same thing and throws up its hands in frustration.

Templates and inline function definitions are the 'exception' to the one-definition-rule because they have to be available to the compiler where they are used. A compiler could not ever inline a function if it doesn't know what the function looks like.

Share this post


Link to post
Share on other sites
Sorry, I didn't know of a difference between declare and define.

And after looking at your code Red Ant, and reading the replies the problem was almost blinking :P. Thanks for the help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!