Bug 24847 - Instantiates un-called copy constructor
Summary: Instantiates un-called copy constructor
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2005-11-14 14:06 UTC by Ivan Godard
Modified: 2021-08-02 09:14 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ivan Godard 2005-11-14 14:06:50 UTC
enum A{b,c};
template<typename T, int i, A x>
struct foo {
explicit    foo(T& t);
explicit    foo(foo<T, 0, x>);
};

int main() {
    int i;
    foo<int, 5, b> v(i);
    }


gets you:

~/ootbc/members/src$ g++ foo.cc
foo.cc: In instantiation of `foo<int, 0,  b>':
foo.cc:10:   instantiated from here
foo.cc:5: error: invalid constructor; you probably meant `foo<int, 0,  b> (const foo<int, 0,  b>&)'

The error arises because it thinks I am instantiating "foo<T, 0,A>(foo<T, 0, A>)", i.e. a copy constructor that is not taking a const reference argument. But I'm not; I'm instantiating the plain constructor "foo<T, int, A>(int&)". 

The second constructor is intended to catch the case: "foo<T, 5, A>(foo<T, 0, A>", i.e. to convert an instance of foo with second argument zero to a foo with any other second argument. It looks like when it is expanding the argument type of the second constructor (i.e. "foo<T, 0, A>") that it is not just parsing the resulting template but also applying the "valid copy constructor?" check for that type. But as that second constructor is never called by anyone it the compiler should just syntax check it (valid) and not semantic check it (invalid for "foo(T, 0, A)" but valid for everything else).

Ivan
Comment 1 Andrew Pinski 2005-11-14 19:32:25 UTC
Comeau also rejects this.  I don't understand why we are trying to instantiate foo<int, 5, b>::foo(foo<int, 0, b>) except to try to match the constructor, so maybe this is invalid after all.  Some one else really needs to look at this.

A weird testcase at best.
Comment 2 Ivan Godard 2005-11-15 00:30:42 UTC
The original was much more sensible - and much bigger :-)

Ivan
Comment 3 Wolfgang Bangerth 2005-11-23 05:21:00 UTC
The second constructor is definitely bogus, but its mere existence 
does no harm. We should only perform the check for sensibility when
we actually instantiate the second constructor.

W.
Comment 4 Andrew Pinski 2021-08-01 23:30:38 UTC
these compiler reject this code:
GCC
MSVC
ICC

Only clang accepts the code.

All of the ones which reject it, all say the constructor is a copy constructor.
Comment 5 Jonathan Wakely 2021-08-02 09:11:06 UTC
Overload resolution considers both constructors, to decide which one v(i) should use. Performing overload resolution on foo<int, 5, b>::foo(foo<int, 0, b>) will cause the instantiation of struct foo<int, 0, b> to see if there is a valid conversion sequence from int to foo<int, 0, b>. Instantiating that type causes the instantiation of foo<int, 0, b>::foo(foo<int, 0, b>) which is indeed an invalid copy constructor.

So the problem is not that it thinks the second constructor is a copy constructor of foo<int, 5, b>, obviously it's not. But it is an invalid copy constructor for foo<int, 0, b> which gets implicitly instantiated.

I don't know how Clang accepts this.
Comment 6 Jonathan Wakely 2021-08-02 09:14:06 UTC
(In reply to Ivan Godard from comment #0)
> The second constructor is intended to catch the case: "foo<T, 5, A>(foo<T,
> 0, A>", i.e. to convert an instance of foo with second argument zero to a
> foo with any other second argument.

You can define that as:

explicit    foo(const foo<T, 0, x>&);

Or:

template<int N, typename = std::enable_if_t<N == 0 && i != 0>
explicit    foo(foo<T, N, x>);

Or in C++20:

explicit    foo(foo<T, 0, x>) requires (i != 0);