Odd error I can't seem to recreate

N

Noah Roberts

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.
 
V

Victor Bazarov

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
 
N

Noah Roberts

On 11/4/2011 2:52 PM, Noah Roberts wrote:

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.
 
G

Gennaro Prota

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.
 
N

Noah Roberts

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?
 
R

Richard Damon

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.
 
N

Noah Roberts

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?
 
G

Gennaro Prota

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)
 
V

Victor Bazarov

[.. 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
 
N

Noah Roberts

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).
 
G

Gennaro Prota

Nope. It complains about the second fun call. No matching function
for call to derived::derived(derived).

That's correct in C++03. The restriction has been lifted in C++11.
 
I

Ian Collins

Here's a further revision of the problem code:
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?

Compiler bug? It compiles fine with gcc 4.5.1.
 
A

Alf P. Steinbach

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());

At this point the service object has been destroyed.

And perhaps the query is running in the background?

Oh, I see below that the problem isn't the obvious asynchronous one that
I thought it was.

Or perhaps the potential problem I mentioned above comes in addition.

OK then, on to the real problem...

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&)

Well, first note that the formal argument is of type `location_service
const&`, while the actual argument is of type `test_service`.

Assumming that `test_service` is derived from `location_service`, or is
the same class.

Then apparently g++ abides by C++98 rules that require an accessible
copy constructor, because C++98 allowed the compiler to introduce any
number of temporaries. This was fixed in C++03. But with C++98 rules, a
copy constructor must be accessible and the result is "as if" it's used,
when you pass a temporary to T const&.

And the test_service copy constructor has non-const formal argument,

test_service&

Hence it cannot be used for copying the actual argument.

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.

It may have been generated due to presence of e.g. std::auto_ptr as member.

Any data member or base class that has T(T&) forces that.

It couldn't (logically) be any other way.

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.

Yes, see above.

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.


Cheers & hth.,

- Alf
 
G

Gennaro Prota

Then apparently g++ abides by C++98 rules that require an accessible
copy constructor, because C++98 allowed the compiler to introduce any
number of temporaries. This was fixed in C++03.

I don't have the standard handy but I'm pretty sure that this
was changed *after* C++03 (I think it all began with core issue
391).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top