Search code examples
c++templatesc++17constexprenable-if

Constexpr function as template parameter for SFINAE


Could anyone clarify why the following does not work:

#include <type_traits>
#include <iostream>

template<typename T, typename U>
constexpr bool is_same_fn()
{
    return std::is_same_v<T, U>;
}

template<typename T, std::enable_if_t<is_same_fn<T, int>(), bool> = true>
void fn2() { std::cout << "True mg\n"; }

template<typename T, std::enable_if_t<!is_same_fn<T, int>(), bool> = true>
void fn2() { std::cout << "False mg\n"; }

int main() {
    fn2<int>();
    fn2<char>();
    return 0;
}

Note that the same thing compiles if rather than using the function, I use directly std::is_same instead as a template parameter.

The errors that I get are:

error C2995:  'void fn2(void)': function template has already been defined
message :  see declaration of 'fn2'
error C3861:  'fn2': identifier not found

The language standard is properly set to: ISO C++17 Standard (/std:c++17). This happens on MSVC 2019, version 16.1.6


Solution

  • They seem to have fixed it after I reported it:

    A fix for this issue has been released in 16.2 Preview 3!