C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

when does a method of a templated class have to be defined?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
David Carlton
Guest





PostPosted: Sun Feb 29, 2004 3:58 am    Post subject: when does a method of a templated class have to be defined? Reply with quote



Is code that does the following (in the order given) legal?

1) Define a templated class, but don't define all the methods in the
class.

2) Define an inlined function that uses one of the declared but not
defined methods in the class from step 1.

3) Define the method used in step 2.

4) Call the function from step 2.

I would assume not, but GCC 3.2, 3.3, and 3.4 all accept the example
code that I'll include after my signature. And I'm not good enough at
reading the standard to figure out whether the code is legal or not.

David Carlton
[email]carlton (AT) bactrian (DOT) org[/email]

#include <cstdio>

template< typename T >
class C {
public:
C( T data ) : data_( data ) {}

T data() const;

private:
T data_;
};

class D {
public:
int foo() { return C< int >( 17 ).data(); }
};

template< typename T >
T
C< T >::data() const {
return data_;
}

int
main() {
D d;

std::printf( "%dn", d.foo() );
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Sun Feb 29, 2004 4:44 pm    Post subject: Re: when does a method of a templated class have to be defin Reply with quote



In message <m3oerismap.fsf (AT) papaya (DOT) bactrian.org>, David Carlton
<carlton (AT) bactrian (DOT) org> writes
Quote:
s code that does the following (in the order given) legal?

1) Define a templated class, but don't define all the methods in the
class.
Fine, and the missing definitions will not be a problem until you do

something that requires they be defined.

Quote:

2) Define an inlined function that uses one of the declared but not
defined methods in the class from step 1.
But inline functions may call other functions based simply on the

promise made by a visible declaration.

Quote:

3) Define the method used in step 2.

So now you supply what is needed and depending where that definition is
either the compiler or the linker can fix up the call in step 2.

Quote:

4) Call the function from step 2.

I would assume not, but GCC 3.2, 3.3, and 3.4 all accept the example
code that I'll include after my signature. And I'm not good enough at
reading the standard to figure out whether the code is legal or not.

Why do you think it is not legal? I fail to see what the problem is.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Carlton
Guest





PostPosted: Mon Mar 01, 2004 7:31 pm    Post subject: Re: when does a method of a templated class have to be defin Reply with quote



David Carlton <carlton (AT) bactrian (DOT) org> wrote


Quote:
Is code that does the following (in the order given) legal?

1) Define a templated class, but don't define all the methods in the
class.

2) Define an inlined function that uses one of the declared but not
defined methods in the class from step 1.

3) Define the method used in step 2.

4) Call the function from step 2.

I would assume not, but GCC 3.2, 3.3, and 3.4 all accept the example
code that I'll include after my signature. And I'm not good enough at
reading the standard to figure out whether the code is legal or not.

Following up on myself: yes, it is legal. I was confused about the
interactions between instantiation and having a definition available.
14.7.1 says that the member function in question gets instantiated
at step 2, but that doesn't meen that it has to be defined by then:
14p8 says "A non-exported template that is neither explicitly
specialized nor explicitly instantiated must be defined in every
translation unit in which it is implicitly instantiated", but it doesn't
put any further restrictions on where in the translation unit that
definition must occur.

David Carlton
[email]carlton (AT) bactrian (DOT) org[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Carlton
Guest





PostPosted: Mon Mar 01, 2004 7:33 pm    Post subject: Re: when does a method of a templated class have to be defin Reply with quote

Here's another example involving methods of templated classes; I
guess I still don't understand the implications of what it means
for a member function of a templated class to be instantiated at
a given point in the code.

Question 1: Is this code legal? If it's not legal, is a compiler
allowed to accept it? (It compiles with GCC 3.2 and 3.4 but
not 3.3, for what that's worth.)

#include <cstdio>

template< typename T >
class D {
public:
D( T *data ) : data_( data ) {}
int foo();

private:
T *const data_;
};

template< typename T >
int D< T >::foo() {
return data_->foo();
}

class C;

class E {
public:
int foo( C *obj ) { return D< C >( obj ).foo(); }
};

class C {
public:
int foo() { return 123; }
};

int main() {
C c;
E e;

std::printf( "%dn", e.foo( &c ) );
}


Question 2: would it become legal if I were to move the definition
of D::foo() after the definition of C?

David Carlton
[email]carlton (AT) bactrian (DOT) org[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.