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

            Bug ID: 78381
           Summary: [c++17] Template Type Deduction from Constructor call
                    with explicit deduction guide leads to constructor
                    ambiguity
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jacek at galowicz dot de
  Target Milestone: ---

I have the following code:

///////////////////////////////////////////////////////////
#include <tuple>
#include <type_traits>

template <typename T>
struct num_wrapper {
    T value;

    explicit num_wrapper(T v) : value{v} {}

    // Works, if i use this one:
    //explicit num_wrapper(const T &v)        : value{v} {}
    //explicit num_wrapper(std::decay_t<T> v) : value{v} {}


    bool shift_right_and_get_lowest_bit() {
        const bool lowest (value & 1);
        value >>= 1;
        return lowest;
    }
};

template <typename T>
num_wrapper(T&&) -> num_wrapper<std::decay_t<T>>;


int main()
{
    num_wrapper a_wrapper (static_cast<const int>(123));
    while (a_wrapper.value != 0) {
        if (a_wrapper.shift_right_and_get_lowest_bit()) {
            // Do something
        }
    }
}

///////////////////////////////////////////////////////////

GCC git version: 357b96452a5fc70011df4f27b5d4dffc5e2f2603

When compiling it, i get the following error:
$ g++-git --std=c++1z gcc_bug.cpp 
gcc_bug.cpp: In function ‘int main()’:
gcc_bug.cpp:26:55: error: call of overloaded ‘num_wrapper(const int)’ is
ambiguous
     num_wrapper a_wrapper (static_cast<const int>(123));
                                                       ^
gcc_bug.cpp:21:1: note: candidate: num_wrapper(T&&)-> num_wrapper<typename
std::decay<_Tp>::type> [with T = const int; typename std::decay<_Tp>::type =
int]
 num_wrapper(T&&) -> num_wrapper<std::decay_t<T>>;
 ^~~~~~~~~~~
gcc_bug.cpp:8:14: note: candidate: num_wrapper(T)-> num_wrapper<T> [with T =
int]
     explicit num_wrapper(T v) : value{v} {}
              ^~~~~~~~~~~


Please note the alternative constructor line which i commented out. If i
comment out the num_wrapper(T) constructor, and select one of the other ctors,
it works.

Reply via email to