![]() |
Odd error I can't seem to recreate
In a significant code base I'm running into this problem:
This works: test_service service; std::string result = job->run_query(service); This does not.: std::string result = job->run_query(test_service()); the run_query function expects a const reference to test_service's base class: struct location_job { virtual ~location_job() {} virtual std::string run_query(location_service const& service) const = 0; }; The error I get from g++4.1.2 is: pipe_processor_test.cpp:142: error: no matching function for call to âtest_service::test_service(test_service)â .../inc/test_service.h:27: note: candidates are: test_service::test_service(bool) .../inc/test_service.h:7: note: test_service::test_service(test_service&) The bool constructor is made by me, but the non-const copy constructor is the compiler's. Line 7 is the opening brace for the class. I can't make any sense of this. Does anyone have ANY idea what might be going on so I can try to narrow it down and create a minimal example to get better help? The attempts I've made so far work fine. Under what conditions in which the first could work might the second not when the parameter is const ref? I can't think of any. Thanks for any help that can be provided. I know it's a reach. |
Re: Odd error I can't seem to recreate
On 11/4/2011 2:52 PM, Noah Roberts wrote:
> In a significant code base I'm running into this problem: > > This works: > test_service service; > std::string result = job->run_query(service); > > This does not.: > std::string result = job->run_query(test_service()); > > the run_query function expects a const reference to test_service's > base class: > > struct location_job > { > virtual ~location_job() {} > > virtual std::string run_query(location_service const& service) const > = 0; > }; Seems like there is some kind of relationship between 'location_service' and 'test_service', but you decided to omit it. Perhaps you could elaborate? Is 'test_service' derived from 'location_service' as private or protected? > The error I get from g++4.1.2 is: > > pipe_processor_test.cpp:142: error: no matching function for call to > âtest_service::test_service(test_service)â > ../inc/test_service.h:27: note: candidates are: > test_service::test_service(bool) > ../inc/test_service.h:7: note: > test_service::test_service(test_service&) Is there the default c-tor for 'test_service'? I presume there is. So, 'test_service' copy constructor takes a non-const reference. To initialize the argument to your 'run_query', a temporary of type 'location_service' has to be created (or has to be able to be created), and I am guessing there is no conversion that the compiler can use. When you supply a "real" object, not a temporary, the derived-to base conversion is used, and all is well. > The bool constructor is made by me, but the non-const copy constructor > is the compiler's. What??? Why would the compiler create a non-const copy c-tor? Or, rather, what have you done to tell it to create that particular form of the copy c-tor? > Line 7 is the opening brace for the class. > > I can't make any sense of this. Does anyone have ANY idea what might > be going on so I can try to narrow it down and create a minimal > example to get better help? The attempts I've made so far work > fine. > > Under what conditions in which the first could work might the second > not when the parameter is const ref? I can't think of any. > > Thanks for any help that can be provided. I know it's a reach. Please try to narrow it down by copying your classes into a separate module and gradually removing all that is irrelevant, while still trying to repeat the compiler behavior. Then post the remaining source, which by that time should be no larger than this: struct loc { loc() {} loc(loc&) {} }; struct test : loc { test(test& t) : loc(t) {} test() : loc() {} }; void foo(const loc&); int main() { foo(test()); // and here you should get an error test t; foo(t); } I don't get the error with Comeau online for this program, but (a) I was just guessing, and (b) that is a different compiler. V -- I do not respond to top-posted replies, please don't ask |
Re: Odd error I can't seem to recreate
On Nov 4, 12:49*pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 11/4/2011 2:52 PM, Noah Roberts wrote: > > The bool constructor is made by me, but the non-const copy constructor > > is the compiler's. > > What??? *Why would the compiler create a non-const copy c-tor? *Or, > rather, what have you done to tell it to create that particular form of > the copy c-tor? > Yes, that bothers me a great deal as well, but for some reason g++ seems to be making non-const copy constructors by default. Here's the class declarations: struct location_service // needs to be at least mockable. { virtual ~location_service() {} enum coord_format { DMS, DECIMAL }; enum location_tech { LEAST, MOST, CELL, AGPS }; typedef std::auto_ptr<service_response> response_t; // pure virtual calls... }; #include "location_service.h" struct test_service : location_service { // overrides of inherited virtuals... test_service(); test_service(bool sleeper); ~test_service(); private: struct impl; std::auto_ptr<impl> pimpl; }; No declaration of copy constructor anywhere. It's not the first time I've seen it and I'm assuming it's some sort of quirk/bug in g++ 4.1.2. Switches are: g++ -g $(INCLUDES) $(LIBFLAGS) $(WIN32_DEF) -DTEST -Wall -Werror - pedantic -Wno-long-long include flags are just a bunch of -I args. For libs I do a couple odd things: -Wl,-Bstatic $(CPPUNIT_LIBS) $(LOG_LIBS) -Wl,-Bdynamic -lpthread But not THAT odd... So I don't know WTF g++ is doing that for. |
Re: Odd error I can't seem to recreate
On Fri, 04 Nov 2011 19:52:56 +0100, Noah Roberts <roberts.noah@gmail.com>
wrote: > In a significant code base I'm running into this problem: > > This works: > test_service service; > std::string result = job->run_query(service); > > This does not.: > std::string result = job->run_query(test_service()); > > the run_query function expects a const reference to test_service's > base class: > > struct location_job > { > virtual ~location_job() {} > > virtual std::string run_query(location_service const& service) const > = 0; > }; > > The error I get from g++4.1.2 is: > > pipe_processor_test.cpp:142: error: no matching function for call to > âtest_service::test_service(test_service)â > ../inc/test_service.h:27: note: candidates are: > test_service::test_service(bool) > ../inc/test_service.h:7: note: > test_service::test_service(test_service&) > > The bool constructor is made by me, but the non-const copy constructor > is the compiler's. Line 7 is the opening brace for the class. > > I can't make any sense of this. Does anyone have ANY idea what might > be going on so I can try to narrow it down and create a minimal > example to get better help? The attempts I've made so far work > fine. > > Under what conditions in which the first could work might the second > not when the parameter is const ref? I can't think of any. > > Thanks for any help that can be provided. I know it's a reach. Try looking at the copy constructors of test_service's members: - the direct and virtual base classes - the non-static data members At least one of them should have a copy constructor without const-qualification on the first parameter, which is why the compiler-generated copy constructor of test_service lacks it as well. -- Gennaro Prota | name.surname gmail.com Breeze C++ (preview): <http://sourceforge.net/projects/breeze/> Do you need expertise in C++? I'm available. |
Re: Odd error I can't seem to recreate
On Nov 4, 1:03*pm, "Gennaro Prota" <gennaro.pr...@gmail.com> wrote:
> On Fri, 04 Nov 2011 19:52:56 +0100, Noah Roberts <roberts.n...@gmail.com> > wrote: > > > > > > > > > > > In a significant code base I'm running into this problem: > > > This works: > > test_service service; > > std::string result = job->run_query(service); > > > This does not.: > > std::string result = job->run_query(test_service()); > > > the run_query function expects a const reference to test_service's > > base class: > > > struct location_job > > { > > * *virtual ~location_job() {} > > > * *virtual std::string run_query(location_service const& service) const > > = 0; > > }; > > > The error I get from g++4.1.2 is: > > > pipe_processor_test.cpp:142: error: no matching function for call to > > âtest_service::test_service(test_service)â > > ../inc/test_service.h:27: note: candidates are: > > test_service::test_service(bool) > > ../inc/test_service.h:7: note: > > test_service::test_service(test_service&) > > > The bool constructor is made by me, but the non-const copy constructor > > is the compiler's. *Line 7 is the opening brace for the class. > > > I can't make any sense of this. *Does anyone have ANY idea what might > > be going on so I can try to narrow it down and create a minimal > > example to get better help? *The attempts I've made so far work > > fine. > > > Under what conditions in which the first could work might the second > > not when the parameter is const ref? *I can't think of any. > > > Thanks for any help that can be provided. *I know it's a reach. > > Try looking at the copy constructors of test_service's members: > > - the direct and virtual base classes > - the non-static data members > > At least one of them should have a copy constructor without > const-qualification on the first parameter, which is why the > compiler-generated copy constructor of test_service lacks it as > well. Thanks. This does appear to be the main issue. There is an auto_ptr in the test_service. I recreated the problem: cat wtf.cpp #include <memory> struct base { virtual ~base() {} }; struct derived : base { std::auto_ptr<int> i; derived() : i() {} //private: //derived(derived const&); }; struct base; struct base2 { virtual ~base2() {} virtual void fun(base const&) const = 0; }; struct derived2 : base2 { void fun(base const&) const {} }; struct base3 { virtual ~base3() {} virtual std::auto_ptr<base2> get() const = 0; }; struct derived3 : base3 { std::auto_ptr<base2> get() const { return std::auto_ptr<base2>(new derived2); } }; int main() { derived3 d3; std::auto_ptr<base2> b2 = d3.get(); b2->fun(derived()); } Now I get all the errors I don't expect that are giving me crap in the original code: g++ wtf.cpp wtf.cpp: In function âint main()â: wtf.cpp:42: error: no matching function for call to âderived::derived(derived)â wtf.cpp:9: note: candidates are: derived::derived(derived&) Victor said: So, 'test_service' copy constructor takes a non-const reference. To initialize the argument to your 'run_query', a temporary of type 'location_service' has to be created (or has to be able to be created), and I am guessing there is no conversion that the compiler can use. .................. As I understood it, because it takes a non-const reference the temporary created by the "test_service()" call should be directly used, rather than a copy of it attempted. Why is it necessary to make a temporary of 'location_service', or 'base' in the above code? |
Re: Odd error I can't seem to recreate
On 11/4/11 3:58 PM, Noah Roberts wrote:
> On Nov 4, 12:49 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote: >> On 11/4/2011 2:52 PM, Noah Roberts wrote: > >>> The bool constructor is made by me, but the non-const copy constructor >>> is the compiler's. >> >> What??? Why would the compiler create a non-const copy c-tor? Or, >> rather, what have you done to tell it to create that particular form of >> the copy c-tor? >> > > Yes, that bothers me a great deal as well, but for some reason g++ > seems to be making non-const copy constructors by default. Here's the > class declarations: > .... > struct test_service : location_service > { > > // overrides of inherited virtuals... > > test_service(); > test_service(bool sleeper); > ~test_service(); > private: > struct impl; > std::auto_ptr<impl> pimpl; > }; > > No declaration of copy constructor anywhere. It's not the first time > I've seen it and I'm assuming it's some sort of quirk/bug in g++ > 4.1.2. > Note that auto_ptr has a non-const copy constructor, as copying an auto_ptr changes the source auto pointer. Therefore any class that has an auto_ptr member will by default have a non-const copy constructor, as it can't make a const copy constructor. |
Re: Odd error I can't seem to recreate
On Nov 4, 11:52*am, Noah Roberts <roberts.n...@gmail.com> wrote:
> Under what conditions in which the first could work might the second > not when the parameter is const ref? *I can't think of any. Here's a further revision of the problem code: cat wtf.cpp #include <memory> #include <iostream> struct base { virtual ~base() {} virtual void f() const = 0; }; struct derived : base { #ifdef HUH private: derived(derived const&); #endif public: derived() {} void f() const { std::cout << "derived::f() \n"; } }; struct base; struct base2 { virtual ~base2() {} virtual void fun(base const&) const = 0; }; struct derived2 : base2 { void fun(base const& b) const { b.f(); } }; struct base3 { virtual ~base3() {} virtual std::auto_ptr<base2> get() const = 0; }; struct derived3 : base3 { std::auto_ptr<base2> get() const { return std::auto_ptr<base2>(new derived2); } }; int main() { derived3 d3; std::auto_ptr<base2> b2 = d3.get(); b2->fun(derived()); } The result of compilation and run without -DHUH: $ g++ wtf.cpp $ ./a.out derived::f() The result with -DHUH: $ g++ -DHUH wtf.cpp wtf.cpp: In function âint main()â: wtf.cpp:14: error: âderived::derived(const derived&)â is private wtf.cpp:47: error: within this context So it appears that the problem is with the temporary being created, and that I don't understand. As far as I knew, const reference allowed you to use the initially created temporary directly. Where's the standard text that explains why it's doing this? |
Re: Odd error I can't seem to recreate
On Fri, 04 Nov 2011 21:03:55 +0100, Gennaro Prota
<gennaro.prota@gmail.com> wrote: > Try looking at the copy constructors of test_service's members: > > - the direct and virtual base classes > - the non-static data members > > At least one of them should have a copy constructor without > const-qualification on the first parameter, which is why the > compiler-generated copy constructor of test_service lacks it as > well. Poorly worded, sorry (although it shouldn't affect understandability). Should have been: Try looking at the copy constructors of test_service's: - direct and virtual base classes - non-static data member types (their elements in case of arrays) -- Gennaro Prota | name.surname gmail.com Breeze C++ (preview): <http://sourceforge.net/projects/breeze/> Do you need expertise in C++? I'm available. |
Re: Odd error I can't seem to recreate
On 11/4/2011 4:07 PM, Noah Roberts wrote:
> [.. seems that auto_ptr is to blame ..] > I recreated the problem: > > cat wtf.cpp > #include<memory> > > struct base > { > virtual ~base() {} > }; > > struct derived : base > { > std::auto_ptr<int> i; > derived() : i() {} > //private: > //derived(derived const&); > }; > > struct base; > struct base2 > { > virtual ~base2() {} > virtual void fun(base const&) const = 0; > }; > struct derived2 : base2 > { > void fun(base const&) const {} > }; > > struct base3 > { > virtual ~base3() {} > virtual std::auto_ptr<base2> get() const = 0; > }; > struct derived3 : base3 > { > std::auto_ptr<base2> get() const { return > std::auto_ptr<base2>(new derived2); } > }; > > int main() > { > derived3 d3; > std::auto_ptr<base2> b2 = d3.get(); > b2->fun(derived()); > } Comeau online compiles it without even a blink. So does VC++ 2010 on my machine. > Now I get all the errors I don't expect that are giving me crap in the > original code: > > g++ wtf.cpp > wtf.cpp: In function âint main()â: > wtf.cpp:42: error: no matching function for call to > âderived::derived(derived)â > wtf.cpp:9: note: candidates are: derived::derived(derived&) > > Victor said: > So, 'test_service' copy constructor takes a non-const reference. To > initialize the argument to your 'run_query', a temporary of type > 'location_service' has to be created (or has to be able to be > created), > and I am guessing there is no conversion that the compiler can use. > ................. > > As I understood it, because it takes a non-const reference the > temporary created by the "test_service()" call should be directly > used, rather than a copy of it attempted. Why is it necessary to make > a temporary of 'location_service', or 'base' in the above code? I may just as well be wrong here. So far I failed to reproduce your issue with another compiler. I don't have access to gcc/g++ at this time. Let's drop the auto_ptr member and try declaring our own copy-c-tor that takes a non-const ref... I've dropped the 'i' member from 'derived' and instead added a copy c-tor with a non-const ref argument, both Comeau online and VC++ still compiled it. Something is funky with g++, I am guessing. Can it compile this: struct base { virtual ~base() {} }; struct derived : base { derived() : base() {} derived(derived& d) : base(d) {} }; void fun(base const& b) { return; } int main() { derived d; fun(d); fun(derived()); } ? V -- I do not respond to top-posted replies, please don't ask |
Re: Odd error I can't seem to recreate
On Nov 4, 1:43*pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> Something is funky with g++, I am guessing. *Can it compile this: > > * * struct base > * * { > * * * *virtual ~base() {} > * * }; > > * * struct derived : base > * * { > * * * *derived() : base() {} > * * * *derived(derived& d) : base(d) {} > * * }; > > * * void fun(base const& b) { return; } > > * * int main() > * * { > * * * *derived d; > * * * *fun(d); > * * * *fun(derived()); > * * } Nope. It complains about the second fun call. No matching function for call to derived::derived(derived). |
| All times are GMT. The time now is 09:34 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.