[Bug c++/87820] New: Explicit user-defined casting inside a template class working in implicit conversion inside function template

fgsimperium at hotmail dot com gcc-bugzilla@gcc.gnu.org
Tue Oct 30 23:21:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87820

            Bug ID: 87820
           Summary: Explicit user-defined casting inside a template class
                    working in implicit conversion inside function
                    template
           Product: gcc
           Version: 8.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fgsimperium at hotmail dot com
  Target Milestone: ---

Let's consider this class definition:

template <typename T>
class Test{
public:
    Test() {}
    explicit operator T() const { return T(); }
};

Then this code below fails to compile (as expected):

int main(){
    Test<unsigned> a(10);
    unsigned j = a; // error: cannot convert 'Test<unsigned int>' to 'unsigned
int' in initialization
}

But this other code compiles fine:

template<typename T>
void test(){
    Test<T> a(10);
    T j = a; // This should be an error
}

int main(){
    test<unsigned>();
}

The second snippet allows an implicit conversion from Test<T> to T, even though
the casting is declared 'explicit'.

It's been tested in every compiler version from 4.5.3 to 8.2. The command line
options are (in Ubuntu 16.04 LTS):

    -std=c++0x -Wall -Wextra -pedantic # c++14 an c++17 have the same behaviour


More information about the Gcc-bugs mailing list