MSVC2015 decltype parameter type in overloaded template function

Is the following program compatible with C ++ 11? If so, do you know about a specific MSVC error that triggers it? and / or possible workaround?

#include <iostream> struct A {}; struct B {}; constexpr A aaa = {}; constexpr B bbb = {}; template <typename T> void foo(T, decltype(aaa)) { std::cout << "a"; } template <typename T> void foo(T, decltype(bbb)) { std::cout << "b"; } // ^ C2995 'void foo(T,unknown-type)': function template has already been defined int main() { foo(0, aaa); foo(0, bbb); } 

If the actual types are replaced with decltype , then it works, but in practice these types are too complex to reproduce, and I would prefer not to have aliases for them.

+6
source share
1 answer

Works for me (VS 2015 / v140) with the following minor modification:

 #include <iostream> struct A {}; struct B {}; constexpr A aaa = {}; constexpr B bbb = {}; using A_type = decltype(aaa); using B_type = decltype(bbb); template <typename T> void foo(T, A_type) { std::cout << "a"; } template <typename T> void foo(T, B_type) { std::cout << "b"; } int main() { foo(0, aaa); foo(0, bbb); } 

But this option gives the same error (not sure what to do with it):

 template <typename T> struct TypeWrapper { using type = T; }; template <typename T> void foo(T, typename TypeWrapper<decltype(aaa)>::type) { std::cout << "a"; } template <typename T> void foo(T, typename TypeWrapper<decltype(bbb)>::type) { std::cout << "b"; } 
+4
source

Source: https://habr.com/ru/post/1012955/


All Articles