Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11262
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Odd error I can't seem to recreate |
| Date | 2011-11-04 15:49 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <j91fkd$7uo$1@dont-email.me> (permalink) |
| References | <bbe45080-3e8d-4e2f-aefc-7ac0906a6872@s7g2000yqa.googlegroups.com> |
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
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 11:52 -0700
Re: Odd error I can't seem to recreate Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-04 15:49 -0400
Re: Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 12:58 -0700
Re: Odd error I can't seem to recreate Richard Damon <news.x.richarddamon@xoxy.net> - 2011-11-04 16:15 -0400
Re: Odd error I can't seem to recreate "Gennaro Prota" <gennaro.prota@gmail.com> - 2011-11-04 21:03 +0100
Re: Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 13:07 -0700
Re: Odd error I can't seem to recreate Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-04 16:43 -0400
Re: Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 13:56 -0700
Re: Odd error I can't seem to recreate "Gennaro Prota" <name.surname@gmail.com> - 2011-11-04 22:16 +0100
Re: Odd error I can't seem to recreate "Gennaro Prota" <gennaro.prota@gmail.com> - 2011-11-04 21:16 +0100
Re: Odd error I can't seem to recreate Noah Roberts <roberts.noah@gmail.com> - 2011-11-04 13:15 -0700
Re: Odd error I can't seem to recreate Ian Collins <ian-news@hotmail.com> - 2011-11-05 12:00 +1300
Re: Odd error I can't seem to recreate "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-05 18:48 +0100
Re: Odd error I can't seem to recreate "Gennaro Prota" <name.surname@gmail.com> - 2011-11-05 22:35 +0100
csiph-web