473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

template problem

I have the following template definition

template<class T> inline T& at(std::vector<T>& v, int pos)
{
#ifdef _MSC_VER
return v.at(pos);
#endif
return v[pos];
}

and is instantiated as below

at(hasMUCVector,mucTag)=true;

where the type information is:

std::vector<bool> hasMUCVector;
int mucTag;

However I am unable to compile the code (g++ v 3.3.1 under Cygwin) and
it reports the following error.

error: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](pos)'
to `bool&'

Any ideas what the problem might be? As an aside if I change the
template definition to

template<class T> inline std::vector<T>::reference at(std::vector<T>& v,
int pos)
{
#ifdef _MSC_VER
return v.at(pos);
#endif
return v[pos];
}

the compiler generates a warning about something being deprecated but
does not flag an error.

Thanks,
Ravi.

Jul 22 '05 #1
3 1929
Ravi escribió:
std::vector<bool> hasMUCVector;
int mucTag;

However I am unable to compile the code (g++ v 3.3.1 under Cygwin) and
it reports the following error.

error: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](pos)'
to `bool&'

Any ideas what the problem might be? As an aside if I change the
template definition to

template<class T> inline std::vector<T>::reference at(std::vector<T>& v,
int pos)
{
#ifdef _MSC_VER
return v.at(pos);
#endif
return v[pos];
}

the compiler generates a warning about something being deprecated but
does not flag an error.


vector <bool> is a peculiar type of vector, can be specialized in order
to be able to use a bit for each element. Thus his reference type can
not be a bool reference.

Regards.
Jul 22 '05 #2
* Ravi <rg**@cse.buffalo.edu> schriebt:
I have the following template definition

template<class T> inline T& at(std::vector<T>& v, int pos)
Make 'pos' a 'std::size_t'.

{
#ifdef _MSC_VER
return v.at(pos);
#endif
return v[pos];
}
What is the purpose of this function? Its effect is to give
checked indexing, with possible exception, when compiled with MSVC,
but not with other compilers. Is that the indended effect?
and is instantiated as below

at(hasMUCVector,mucTag)=true;

where the type information is:

std::vector<bool> hasMUCVector;


This is a bit problematic. std::vector<bool> is a specialization
that's meant to minimize storage by using one bit per bool. Obviously
you cannot treat one single bit as a 'bool', in general, and so a
'bool&' cannot serves as std::vector<bool>::reference (see §23.2.5).

In short, your 'at' function cannot return a 'T&' if you intend to
use it for vector<bool>.

Instead you can let it return a std::vector<T>::reference.

Jul 22 '05 #3
On Fri, 27 Feb 2004 11:57:33 -0500, Ravi <rg**@cse.buffalo.edu> wrote:


error: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](pos)'
to `bool&'

[snip]

Julian explained the problem. A work-around, if you're not too concerned
about the memory-efficiency of containers of bool, might be to use deque
instead of vector in your template.
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
5
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.