c++template-argument-deduction

How can this compile?


How does the second call to meow() compile here?

#include <iostream>

struct F {
    struct E {
        using is_it_safe = bool;
    };

    E e;
};

template<typename T, typename = typename T::E::is_it_safe>
const void* meow(T const& t)
{
    return &t;
}

int main() {
    F f;

    meow(f); // meow<F, bool>
    meow(f.e); // meow<F::E, bool> ??
}

godbolt link

F::E doesn't have a nested E, so how does the compiler deduce the second template parameter?


Solution

  • E has an injected-class-name, so that E::E is itself (E).

    So F::E::E::is_it_safe is bool, since F::E::E is the E class:

    F::E::E::is_it_safe x;
    static_assert(std::is_same_v<decltype(x), bool>);