templates and inline

J

Jeff Williams

The common method of defining template classes and functions is to put the
definition and declaration into the same header file. Or at least I believe
it to be the common method and it is certainly the one I use.

This leaves me with a question.

As example, consider the following class.

template<class T>
class foo
{
public:
void my_big_func()
{
// non trivial code goes here that is something you wouldnt want
// to be inline
}
};


Now my question is, since all class functions declared and defined within
the class declaration are taken to be inline, does that mean all my template
class functions will be inline? If so how can I prevent this? and how can
I make the ones I want to be inline be inline.

My thought is I can not declare my template class memebers within the class
declaration, but this is extremely tedious and hard to maintain.

Jeff
 
A

Alf P. Steinbach

The common method of defining template classes and functions is to put the
definition and declaration into the same header file. Or at least I believe
it to be the common method and it is certainly the one I use.

This leaves me with a question.

As example, consider the following class.

template<class T>
class foo
{
public:
void my_big_func()
{
// non trivial code goes here that is something you wouldnt want
// to be inline
}
};


Now my question is, since all class functions declared and defined within
the class declaration are taken to be inline, does that mean all my template
class functions will be inline?

They're textually inline but not declared 'inline', and may or may not be
inlined in the compiled program, at the compiler's discretion.


... how can I make the ones I want to be inline be inline.

You cannot, but you can give the compiler a hint via 'inline'.
 
T

tom_usenet

The common method of defining template classes and functions is to put the
definition and declaration into the same header file. Or at least I believe
it to be the common method and it is certainly the one I use.

This leaves me with a question.

As example, consider the following class.

template<class T>
class foo
{
public:
void my_big_func()
{
// non trivial code goes here that is something you wouldnt want
// to be inline
}
};


Now my question is, since all class functions declared and defined within
the class declaration are taken to be inline, does that mean all my template
class functions will be inline? If so how can I prevent this? and how can
I make the ones I want to be inline be inline.

Define the members outside the class definition, and use inline when
you want inline. Some compilers think (or know) that they know better,
and might ignore the use of inline, and not inline or inline at their
own discretion.
My thought is I can not declare my template class memebers within the class
declaration, but this is extremely tedious and hard to maintain.

You have to declare them in the class in C++, but you can define them
outside. You can't call that hard to maintain, unless you always
define all your methods inside the class definition for non-templates
too!

Tom
 

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

Similar Threads

templates?? 2
Proposed Standard Change: inline this 8
templates 10
inline, templates, 1
templates help 1
gcc inline memcpy 7
templates 4
templates, nesting and using problem 0

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top