473,809 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<boo l> 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<bo ol,
_Alloc>::operat or[](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 1962
Ravi escribió:
std::vector<boo l> 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<bo ol,
_Alloc>::operat or[](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.buffa lo.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<boo l> hasMUCVector;


This is a bit problematic. std::vector<boo l> 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<boo l>::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.buffa lo.edu> wrote:


error: could not convert `std::vector<bo ol,
_Alloc>::operat or[](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
2612
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 the code below. The function template < typename T >
6
2341
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 data from the class. Here's the gist of the code: template <class T> struct NodeType { T data; NodeType<T> * link;
7
2482
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
7
2138
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
4471
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 think this is really the issue. Here is a description of my problem. MathLibrary.dll: - SpaceTimeVector (uses various stuff from this dll)
5
1720
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 i havent correctly understood related to template specialization or is it some problem related to the compiler. Following is the code..
2
2765
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 parameter, e.g. template<class T> - parameters with *simple* types like int, e.g. template<int size> - template parameter, e.g. template< template<class> class A > the latter beeing a type specifier, not an instance of A.
3
3942
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. template<class Type> class base { public: base& operator/=(const base&); Type *image;
19
7944
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 macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
3
3763
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.