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 

typedefs and references

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





PostPosted: Thu Jul 22, 2004 10:42 pm    Post subject: typedefs and references Reply with quote



This simple example doesn't compile:

#include <vector>

template<typename cont_t>
void noop (cont_t in) {
for (typename cont_t::iterator i = in.begin(); i != in.end(); i++) ;
}

void F() {
std::vector<int> v;
noop<std::vector (v);
}
g++34 -c Test.cc
Test.cc: In function `void noop(cont_t) [with cont_t = std::vector<int,
std::allocator&]':
Test.cc:10: instantiated from here
Test.cc:5: error: `std::vector<int, std::allocator&' is not a class,
struct, or union type

The problem can be solved using boost::remove_reference. This is tedious.
Is it reasonable that this code should be rejected? If a type T has a
typedef, then would T& not also have that typedef?

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





PostPosted: Fri Jul 23, 2004 2:12 am    Post subject: Re: typedefs and references Reply with quote



On Thu, 22 Jul 2004 15:42:54 -0700, Neal D. Becker wrote:

Quote:
This simple example doesn't compile:

#include <vector

template void noop (cont_t in) {
for (typename cont_t::iterator i = in.begin(); i != in.end(); i++) ;
}

I think you want to define noop as taking a reference. Containers are
generally expensive to copy. Additionally, if there is no change on
the container, it better be const-reference:

template void noop(cont_t const & in);

Quote:

void F() {
std::vector<int> v;
noop<std::vector (v);
}

I think you are putting the '&' at the wrong place. The type that noop
will use is std::vector<int>. How an object of that type is passed to
noop is defined in the function signature.

I think that making the 'cont_t const & in' change above is all you
need in this case.

Quote:
g++34 -c Test.cc
Test.cc: In function `void noop(cont_t) [with cont_t = std::vector<int,
std::allocator&]':
Test.cc:10: instantiated from here
Test.cc:5: error: `std::vector<int, std::allocator&' is not a
class, struct, or union type

The problem can be solved using boost::remove_reference. This is
tedious. Is it reasonable that this code should be rejected? If a type
T has a typedef, then would T& not also have that typedef?

typedef just renames an existing type. A type can't "have a typedef",
but may "have an alias through the use of typedef."

I see T& as a reference to T; that's all. Maybe I am misunderstanding
your question...

Ali

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

Back to top
Dieter Beaven
Guest





PostPosted: Fri Jul 23, 2004 9:53 am    Post subject: Re: typedefs and references Reply with quote



Hi Neal,

Looking at the iterator declaration in noop's for loop, I think the
compiler would interpret it as the following:

cont_t::iterator => std::vector<int, std::allocator&::iterator

i.e. the statement is asking for the iterator_type belonging to a
reference, not the iterator type of a std::vector<int,
std::allocator.

One solution would be to declare void noop( cont_t& in ), and dispense
with the reference in the template instantation. Else maybe an
appropriate typedef within noop() to specify to the compiler exactly
what you intend.

I would favour the former unless you have a good reason to want to
declare template instanciations of references. Things like
std::vector<int&> tend not to compile due to reference to reference
issues; which aren't currently supported in C++. If you need to
support references in template instanciations Alexandrescu's Modern
C++ Design has some useful techniques.

Regards,

Dieter

[ 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.