LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2005, 05:41 AM   #1
(tm)
Member
 
Registered: Mar 2004
Location: Wrocław, Poland
Distribution: Fedora 2
Posts: 31

Rep: Reputation: 15
Defining a non-inline function inside class definition


Hi!

Does anybody know if it is possible to define a non-inline function inside class definition?
In other words:

class A {
void f() {/*I don't want this function 2 b inline*/}
}

I HAVE TO put the function defintion inside class definition (it's connected with VC++ bug with template functions).

Thanks
tm
 
Old 04-26-2005, 10:12 PM   #2
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
I cannot see how your code example is related to the use of template. Besides, it is a bit confusing that you used 'definition' to refer to a piece of code which is actually a 'declaration' of a class...

1) how this class A is related to the template bug you mention about?
2) do you have the link to msdn which says something about the particular template bug you are talking about?
3) Generally speaking the function definition of a template class has to be visible to the compiler during template instantiation. So, is it what you want to avoid to do?
4) Generally speaking you have to give 'inline' keyword to make a function inline. Putting them in header function alone does not necessarily make them inline...
 
Old 04-26-2005, 11:26 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ya... if the question your asking is what you appear to be asking then the answer is yes... just don't label the thing inline.
 
Old 05-11-2005, 08:11 AM   #4
(tm)
Member
 
Registered: Mar 2004
Location: Wrocław, Poland
Distribution: Fedora 2
Posts: 31

Original Poster
Rep: Reputation: 15
Hi!

Cheers for the answers!

I don't want to make a function inline. I want to prevent it from being inline.

My problem is related to the fact that you have to declare template functions of template classes in the class declaration - you cannot delcare them outside the class declaration. VC++ 6 won't copile such code.
Check it out:
//INFO: C++ Standard Noncompliance Issues with Visual C++ .Net
//http://support.microsoft.com/kb/241949/EN-US/
//check also: http://inspira.by.ru/ironwork/msvcpp...n_Out-Of-Class

Code:
template<class T> class A 
{
template<class U> void f( U& u ) {};
}
//OK

template<class T> class B
{
template<class U> void g( U& u );
}
template<class T> template<class U> void g( U& u ) {}
//this won't compile with VC++ 6
This is why i wandered if there's a way to make a function defined in the class definition NOT an inline function.


You're right, I should have put a more detailed description of the problem, but:
>Besides, it is a bit confusing that you used 'definition' to refer to a piece of code which is actually a 'declaration' of a class...
1. I was talking about FUNCTION definition.
2. Exapmle I gave was a definition of a class (which by the way in this case is its declaration as well).


Cheers!
 
Old 05-11-2005, 08:17 AM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I think there is confusion here surrounding the term "inline". In C and C++ inline is a keyword used to tell the compiler to compile the code inline everywhere it is called which means the function will have no overhead (jumping, and stack operations). You use inline functions to allow a better visual flow to your code without causing a performance penalty.

Now, what you are describing is simply not allowed by C++ as far as I know. If you want a function to be a member of a class you have to define it in the class. You can always define the implementation outside, but the prototype must exist in the class body for it to be a member of the class.
 
Old 05-11-2005, 08:21 AM   #6
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ok... ignore the second paragraph of what I wrote above and check out this code:

Code:
template<class T> class A 
{
template<class U> void f( U& u ) {};
}
//OK

template<class T> class B
{
template<class U> g( U& u );
}
template<class U> B::g( U& u ) {}
I was a little confused by your template<class U> void g( U& u); so I simplified it a touch since a C++ function can't be both void and return a value... does this help?
 
Old 05-11-2005, 09:14 AM   #7
(tm)
Member
 
Registered: Mar 2004
Location: Wrocław, Poland
Distribution: Fedora 2
Posts: 31

Original Poster
Rep: Reputation: 15
The confusion:
You are right, but I d

Actually the proper code should look like this
Code:
template<class T> class B
{
template<class U> void g( U& u );
}

template<class T> template<class U> void B<T>::g( U& u ) 
{
/*bla bla*/
}
//this won't compile with VC++ 6, but will compile with gcc and other compilers
The example form the working code (obviously comments are removed):
Code:
template<class T> class CTLTestCaseUnit : public CTLTestCase
{
public:
T m_TestedClassObject;
template<class U, class X> void StartTestGetGetSetPointersMethods
	( U& a_rObject 
	, void ( T::*a_pSetFunction )( X* const )
	/*...*/
	)
	{
	/*...*/
	}
}
I wan to put function definition outside class definition but VC++ 6 doesn't allow it (mensioned bug). I want to prevent it being inline at least.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
inline function means alaios Programming 9 08-31-2005 07:03 AM
inline function and macros in C++ carthyc Linux - General 2 05-14-2005 12:16 AM
[SOLVED] C++ inline function: Use or don't use? druuna Programming 7 09-13-2004 04:08 PM
problem on dynamic function call inside a class! antony_csf Programming 0 06-29-2004 10:15 PM
gcc: compilation with extern inline function philipsyyy Linux - Software 0 10-13-2002 11:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration