考虑以下代码:
#include <type_traits>
template <typename T> constexpr bool g() { return true; }
template <typename T> std::enable_if_t< g<T>()> f() {}
template <typename T> std::enable_if_t<!g<T>()> f() {}
int main() { f<int>(); }
(编译器资源浏览器)
这对于各个版本的GCC和Clang都可接受,但是它不能用于MSVC,由于编译错误导致无法编译,将会出现错误提示信息。
1>D:\x.cpp(5,49): error C2995: 'enable_if<0,void>::type f(void)': function template has already been defined
1>D:\x.cpp(4): message : see declaration of 'f'
1>D:\x.cpp(4,49): error C3861: 'f': identifier not found
第一个错误信息让我想到了ODR违规,但如果这个程序是I形式的NDR的话,我需要帮助理解为什么会这样。我已经查看了标准草案中的[temp.over.link],但我不确定自己是否正确地解释了它。据我理解,程序是可以的,因为这些函数模板具有不同的签名。
如果这个程序正确的话,为什么MSVC会拒绝它呢?