 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ravi Guest
|
Posted: Fri Feb 27, 2004 4:57 pm Post subject: 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.
|
|
| Back to top |
|
 |
Julián Albo Guest
|
Posted: Fri Feb 27, 2004 5:09 pm Post subject: Re: template problem |
|
|
Ravi escribió:
| Quote: | 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.
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Fri Feb 27, 2004 5:22 pm Post subject: Re: template problem |
|
|
* Ravi <rg27 (AT) cse (DOT) buffalo.edu> schriebt:
| Quote: | I have the following template definition
template<class T> inline T& at(std::vector<T>& v, int pos)
|
Make 'pos' a 'std::size_t'.
| Quote: | {
#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?
| Quote: | 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.
|
|
| Back to top |
|
 |
Leor Zolman Guest
|
Posted: Fri Feb 27, 2004 5:22 pm Post subject: Re: template problem |
|
|
On Fri, 27 Feb 2004 11:57:33 -0500, Ravi <rg27 (AT) cse (DOT) buffalo.edu> wrote:
| Quote: |
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
[email]leor (AT) bdsoft (DOT) com[/email]
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
|
|
| Back to top |
|
 |
|
|
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
|
|