所以ms 在线编译器无法编译这个(就像我家 VS2012 和 SP1(+ 十一月包)一样),而clang 和现代 gcc 可以。谁能告诉我VS中缺少什么C ++ 11功能,有什么办法吗?
#include <iostream>
#include <utility>
#include <type_traits>
struct A {
int x;
void a() {
std::cout << "an a! " << x << "\n";
}
};
struct B {
double x;
double b(double k) {
std::cout << "b! " << x << ", " << k << "\n";
return x - k;
}
void b() {
std::cout << "b! " << x << ", ?\n";
}
};
struct C {
A *_first__;
B *_second__;
C(A * _first__, B * _second__):_first__(_first__), _second__(_second__) {
} template < typename K, typename ... T > static auto _a_caller__(K * k, T && ... args)->decltype(k->a(std::forward < T > (args) ...)) {
return k->a(std::forward < T > (args)...);
}
template < typename...T > auto a(T &&...args)->decltype(_a_caller__(_first__, std::forward < T > (args)...)) {
return _a_caller__(_first__, std::forward < T > (args)...);
}
template < typename...T > auto a(T &&...args)->decltype(_a_caller__(_second__, std::forward < T > (args)...)) {
return _a_caller__(_second__, std::forward < T > (args)...);
}
template < typename K, typename...T > static auto _b_caller__(K * k, T && ... args)->decltype(k->b(std::forward < T > (args) ...)) {
return k->b(std::forward < T > (args)...);
}
template < typename...T > auto b(T &&...args)->decltype(_b_caller__(_first__, std::forward < T > (args)...)) {
return _b_caller__(_first__, std::forward < T > (args)...);
}
template < typename...T > auto b(T &&...args)->decltype(_b_caller__(_second__, std::forward < T > (args)...)) {
return _b_caller__(_second__, std::forward < T > (args)...);
}
};
int main() {
A a {12};
B b {24};
C c (&a, &b);
c.a();
c.b();
std::cout << c.b(2445) << std::endl;
}
错误:
testvc.cpp
--\testvc.cpp(38) : error C2535: 'unknown-type C::a(T &&...)' : member function already defined or declared
--\testvc.cpp(33) : see declaration of 'C::a'
--\testvc.cpp(47) : error C2535: 'unknown-type C::b(T &&...)' : member function already defined or declared
--\testvc.cpp(42) : see declaration of 'C::b'
--\testvc.cpp(56) : error C2893: Failed to specialize function template 'unknown-type C::a(T &&...)'
With the following template arguments:
''
--\testvc.cpp(57) : error C2893: Failed to specialize function template 'unknown-type C::b(T &&...)'
With the following template arguments:
''
--\testvc.cpp(58) : error C2893: Failed to specialize function template 'unknown-type C::b(T &&...)'
With the following template arguments:
'int'