SEARCH YOUR SOLUTION HERE  

Use of operator T const& to get a reference to self

Given the following program:

#include

class FG
{
public:
explicit FG( std::locale const& l )
: myLoc( &l ) {}

template< typename F >
operator F const&() const
{
return get< F >( *myLoc, &std::use_facet ) ;
}

private:
std::locale const* myLoc ;

template< typename F >
F const& get( std::locale const& l,
F const& (*f)( std::locale const& ) ) const
{
return (*f)( l ) ;
}
} ;


FG
getF( std::locale const& l )
{
return FG( l ) ;
}

void
f()
{
std::ctype< char > const& ct = getF( std::locale() ) ;
}

G++ (4.1.0) instantates the template operator F const& to get
the const reference needed to call the copy constructor (which
of course fails to compile, since use_facet is not legal).
Providing a non-template:

operator FG const&() const { return *this ; }

solves the problem, but is g++ correct here I would have
expected the temporary "FG( l )" to bind directly to the const
reference of the (compiler generated) copy constructor.

Or maybe my question is: is this intentional Given 8.5.3/5,
"[...]If the initializer expression [...]-- has a class type [it
does] and can be implicitly converted to an lvalue of type "cv3
T3", where "cv1 T1" is reference-compatible with "cv3 T3" [...]
then [...] the reference is bound to the lvalue result of the
conversion [...]" But it doesn't seem at all natural to have a
user defined conversion called for a copy.

--
James Kanze (GABI Software) mailto:james.kanze
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Posted On: Monday 5th of November 2012 01:22:54 AM Total Views:  198
View Complete with Replies




Related Messages:

dubious use of friendship   (169 Views)
I saw some code where a struct is declared with all members public (not explicitly so but by omission -- the word private never appears). Then some operators are declared as friends of this struct. Is there ever any reason to do this Obviously, from a legal and algorithmic standpoint, such code is redundant. However, I don't feel ready to condemn it right away. For example, a future programmer might want to declare some of the members private which would make the friendship necessary. Furthermore, it signals that the named operators will operate on the members of the struct which may be an aid to understanding. Any opinions Paul Epstein
Is this the wrong way to use std::list?   (110 Views)
So I have a class: class Client { unsigned int ClientID; .... }; class MyListenSocket { ... AddClient( Client *pClient); RemoveClient( unsigned int ID ); std::list m_listClients; }; To add clients to the list, AddClient is called: MyListenSocket::AddClient( Client *pClient ) { m_listClients.push_back( *pClient ); } But a client can be erased from anywhere on the list. I wrote this function: MyListenSocket::RemoveClient( unsigned int ID ) { std::list::iterator it; for( it = m_listClients.begin(); it != m_listClients.end(); ++it ) { if ( it->ClientID() == ID ) { m_listClients.erase( it ); break; } } } The problem is that this seems to corrupt the heap in my program. I know that the iterator is corrupted when I use the erase command, but why would that corrupt the heap Is this not the way to delete an item from the middle of a list Should I not be using ::list for this type of purpose
Excuse me!!   (123 Views)
Excuse me!!. Would you stop for a moment! O...man...Haven't you thought-one day- about yourself Who has made it Have you seen a design which hasn't a designer ! Have you seen a wonderful,delicate work without a worker ! It's you and the whole universe!.. Who has made them all !! You know who .. It's "ALLAH",prise be to him. Just think for a moment. How are you going to be after death ! Can you believe that this exact system of the universe and all of these great creation will end in in nothing...just after death! Have you thought, for a second, How to save your soul from ah's punishment! Haven't you thought about what is the right religion! Read ... and think deeply before you answer.. It is religion of Islam. It is the religion that Mohammad-peace upon him- the last prophet, had been sent by. It is the religion that the right Bible- which is not distorted-has preached. Just have a look at The Bible of (Bernaba). Don't be emstional. Be rational and judge.. Just look..listen...compare..and then judge and say your word. We advise you visiting : http://www.islam-guide.com http://www.thetruereligion.org http://www.it-is-truth.org http://www.beconvinced.com http://www.plaintruth.org http://english.islamway.com http://www.todayislam.com http://www.prophetmuhammed.org http://www.islamtoday.net/english/ http://www.islamunveiled.org http://www.islamic-knowledge.com
Re: use of STL Container   (123 Views)
On Dec 12, 9:11 am, omveer wrote: > > When should I use vector, map, list, deque, set and hash_map. > >
Why the std::search(b, e, b2, e2) use Forward Iterator?   (104 Views)
, Why the search() function use Forward Iterator I think Input Iterator is enough, I need read only. In such kind of situations, how should I choose iterator type
Re: Compiler error when trying to use container classes   (143 Views)
Ok, I concur with all three of you. Using the std qualifier let's the test project compile fine. But that doesn't fix the problem in my original project. It still issues the error in the header. So the only thing that we can see now is, that it has nothing to do with the deque implementation. I still believe that this is some configuration issue, either with my project or with the RAD Studios C++ Builder in general. Do you agree
How to find which objects use most of the memory   (204 Views)
. Is there any tool which could help finding out which objects consume most of the memory I was thinking something which would generate a report like: ClassA 120003 instances 3MB ClassB 4550 instances 1MB and so on. I used massif of valgrind but it doesn't do what I need.
newbie howto use QGraphicsView in QT4 dialogs   (196 Views)
I have created a dialog with a QGraphicsView object called graphView on my dialog using designer. So far I have been unable to display any information on the dialog, I have taken the following approach; In the dialog constructor I created a scene for viewing QGraphicsScene thescene(-1000,-1000,2000,2000); // create a scene thescene.addtext("message added here."); I now attempted to connect the scene with the QGraphcsView item on the dialog using; graphicsView.setScene(&thescene); // attempt to view the scene graphicsView.show(); // attempt to display the text message onto the dialog QGraphicsView window. This approach is not working, a pointer in the right direction would be greatly appreciated. regards, Carlton Matthew
Why does this cause access error reading null pointer?   (305 Views)
Microsoft Visual C++ .net 2003. I was just curious what numeric_limits for std::string would give me so wrote this: #include #include #include int main() { std::cout
Why moving a function from protected to public area in class causes error C2556   (147 Views)
Why moving a function from protected to public area in class causes error C2556 - overloaded function differs only by return type I have a protected function called GetState like this: enum EState GetState(void) const throw(); If I move this function from protected: to public: I get this compile error: xbasicsocket.cpp(247) : error C2556: 'enum CXBasicSocket::EState __thiscall CXBasicSocket::GetState(void) const' : overloaded function differs only by return type from 'enum EState __thiscall CXBasicSocket::GetState(void) const' \xbasicsocket.h(154) : see declaration of 'GetState' \xbasicsocket.cpp(247) : error C2371: 'GetState' : redefinition; different basic types \xbasicsocket.h(154) : see declaration of 'GetState' In the cpp file it is declared like this: enum CXBasicSocket::EState CXBasicSocket::GetState( ) const throw() { return m_eState; } Why am I geting this problem How do I resolved
STL sort with user defined data type   (152 Views)
I have been looking in the archives and as of yet have not found an answer to my problem. Class B has four members, and class A is derived from class B. Class A only adds one new member to class B. Class A has '', '==' and '=' overloaded, as well as a copy constructor. Class B only has the copy constructor and assignment operator overloaded. I create a vector of A objects, and stuff them with random values. When I sort the vector of A objects only the member unique to the A object changes. For example say I have three A objects in a vector blah blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 1} blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 0} blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 } I expect blah[0] {one = 2 two = 0.7 three = 0.2 mysortvar = 0} blah[1] {one = 5 two = 0.8 three = 0.1 mysortvar = 1} blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 } but I get blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 0} blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 1} blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 } Note:ONLY THE mysortvar values change. could someone clue me into what I'm doing wrong
The extern "C" construct - does it allow the use of C-style (int-based) enums?   (132 Views)
, I am trying to include a header file which was written in C, but my project is in C++. Even when I wrap the include statement or the entire header files with extern "C" (used according to the instructions on c++-faq-lite), I get the same error, which is this: invalid conversion from `int' to `DmtxDirection' where DmtxDirection is an enum with integer values inside (obviously). The offending function: static DmtxDirection TurnCorner(DmtxDirection dir, int turn) { DmtxDirection newDir; newDir = (turn == DMTX_TURN_CW) 0x0f & (dir >> 1) : 0x0f & (dir
Need user-defined constructor for creating const objects?   (205 Views)
In the following piece of code, why is the implicit default constructor not provided (or not sufficient) to create a const object struct A { //A(){} }; int main() { A a; const A a1; } Comeau online gives the following error: "const variable "a1" requires an initializer -- class "A" has no explicitly declared default constructor". If I uncomment the default constructor the compilation happens successfully - but will that be really different from the one that the class would have got by default -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] , { extraneous linebreaks removed. -mod } On Mon, 11 Jun 2007 15:40:32 CST, Abhishek Padmanabh wrote: >In the following piece of code, why is the implicit default >constructor not provided (or not sufficient) to create a const object > > struct A > { > //A(){} > }; > > int main() > { > A a; > const A a1; > } > >Comeau online gives the following error: "const variable "a1" requires >an initializer -- class "A" has no explicitly declared default >constructor". If I uncomment the default constructor the compilation >happens successfully - but will that be really different from the one >that the class would have got by default FWIW, the MSVC++ compiler doesn't complain (Version 13.10.3052 for 80x86, or the free "toolkit 2003" compiler). But try this instead: int main() { A a; const A a1 = A(); } If I give your struct a member (e.g. int) without a default constructor, an executable is still produced; however, the following warning is issued: test_POD.cpp(10) : warning C4269: 'a1' : 'const' automatic data initialized with compiler generated default constructor produces unreliable results That makes sense. -- Bob Hairgrove NoSpamPlease@Home.com -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
How to detect read or write access to a user defined array?   (182 Views)
Hi Suppose that I want to create an array of read only items I overload the [ ] operator. How can I detect if I am being called within a read context foo = Array[23]; or within a write context Array[23] = foo; As far as I remember, this is not possible in C++.
std::set lexicographic comparisons don't seem to use comparator   (178 Views)
I'm having some trouble with comparing two std::sets set a; set b; if (a < b) ... fails and complains about not finding a mach for operator
How to use functions of given dll?   (116 Views)
I am using VS 2003. I have a dll. I need to create a simple program and just call the functions in this dll and display the result. How do I use the dll Thank you ] Trupti
N2284 Clause 17 editorials   (120 Views)
Some nitpicking. :-) Clause 17 uses the phrase "clauses 18 through 27, and Annex D", in numerous places, to refer to the library part of the standard. However, there are now more than 27 clauses in the current draft. In 17.4.1.2 [headers] tables 13 and 14 have had new header names added. The sentence immediately before each table states the old number of headers though. In 17.4.4.7 [derivation], in the second but last bullet, the word non-virtual appears as virtual "" non-. A make-up error, or perhaps a pdf conversion problem Bo Persson --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
I use Visual Studio Professional 6.Why i can't see any of the UI tools to make interfaces?   (255 Views)
I use Visual Studio Professional 6.Why i can't see any of the UI tools to make interfaces
generic user defined object parameter   (205 Views)
I am trying to pass an object as an argument, but I want to be able to specify the type of that object at runtime. To make things a little more clear, This is what i'm trying to accomplish: bool (*funcPT)(int num, Object myObject); //I want Object to not be bound to a certain type until runtime At runtime the type will be known (via config file) and will not change during program execution. I know you can pass generic parameters with a void pointer but i'm not sure on how to find the type of object it is once inside the function. Could I use sizeof(myObject) to distinguish the type I tried searching through google but couldn't find any concrete info on this (maybe I was looking in the wrong places)
Is it safe to use UTF-8 in comments?   (110 Views)
I am not familiar with the UTF-8 encoding, but I know that it encodes certain characters with up to four bytes. Is it safe to use UTF-8 encoded comments in C++ source files For example, is there a remote possibility that some multi-byte character, when interpreted byte-by-byte, will contain */ and close the comment Or is there something else that can go wrong