473,326 Members | 2,110 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,326 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 1925
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.