C++ VS2012 SP1(11月包)未知类型错误(类似于C::a(T&;))
所以(就像我家的墙一样,带SP1的VS2012(+11月包))同时。有人能告诉我VS中缺少了什么C++11功能吗?有没有办法C++ VS2012 SP1(11月包)未知类型错误(类似于C::a(T&;)),c++,visual-studio,c++11,visual-studio-2012,C++,Visual Studio,C++11,Visual Studio 2012,所以(就像我家的墙一样,带SP1的VS2012(+11月包))同时。有人能告诉我VS中缺少了什么C++11功能吗?有没有办法 #include <iostream> #include <utility> #include <type_traits> struct A { int x; void a() { std::cout << "an a! " << x << "\n"; }
#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;
}
这很复杂。我猜,GCC和CLANG正在使用SFINAE来消除这两个函数模板的歧义,乍一看这两个模板是模棱两可的。让我们看一个例子:调用
c.b(2445)
我要把类型和实际参数弄乱一点,但我希望我的意思是可以理解的
auto b(int args)->decltype(\u b\u caller\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuustd::forward(args))。我猜,GCC和CLANG正在使用SFINAE来消除这两个函数模板的歧义,乍一看这两个模板是模棱两可的。让我们看一个例子:调用c.b(2445)
我要把类型和实际参数弄乱一点,但我希望我的意思是可以理解的
实例化第一个函数模板,意思是
auto b(int-args)->decltype(\u b\u caller\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuufirst,std::forward(args))
,这反过来又实例化了\u b\u caller[此答案已更新。请参阅本文末尾的编辑]
我把这个问题归结为:
#包括
结构A{ag(int){返回A();}};
结构B{bg(){return B();}};
结构C
{
样板
自动f(Ts…Ts)->decltype(A().g(Ts…)
{std::cout[此答案已更新。请参阅正文末尾的编辑]
我把这个问题归结为:
#包括
结构A{ag(int){返回A();}};
结构B{bg(){return B();}};
结构C
{
样板
自动f(Ts…Ts)->decltype(A().g(Ts…)
{std::你能把代码和错误信息粘贴到问题中吗?顺便说一句,包含\uuuu
的名称是保留的,不应该使用。你应该创建一个我想知道为什么它会在GCC和Clang上编译:你定义了两对重载(对于a()
和b()
在C
中)只有回报不同value@AndyProwl:这是SFINAE。调用c.a()
时,第二个重载无法将\u second
替换为\u a_调用者
的参数,因为没有B::a
;因此只有第一个重载被实例化。如果两者都有(或两者都没有),则代码应该无法编译A
和B
有A成员A
。请将代码和错误消息粘贴到问题中好吗?顺便说一句,包含\uuuu
的名称是保留的,不应该使用。您应该创建一个我想知道为什么会在GCC和Clang上编译:您定义了两对重载(对于A()
和C
中的b()
),它们只因返回而不同value@AndyProwl:这是SFINAE。调用c.a()时
,第二个重载无法将\u second
替换为\u a\u调用方
的参数,因为没有B::a
;因此只实例化第一个重载。只有当a
和B
都有a成员a
时,代码才会编译失败。第二个f
是“格式错误,NDR”,因为它仅在模板参数包为空时有效。@Johanneschaub litb:正在做我的语言律师作业:这是因为14.6/8,其中说“如果可变模板的每个有效专门化都需要一个空模板参数包,则模板定义格式错误,不需要诊断。”。“?请提交一份错误报告,让我们看看正式答案。@myWallJSON:我不能:这不是错误。请看我编辑的答案。第二个f
是“格式错误,NDR”,因为它只有在模板参数包为空时才有效。@Johanneschaub litb:正在做我的语言律师作业:这是因为14.6/8,它说“如果可变模板的每个有效专门化都需要一个空的模板参数包,则模板定义格式不正确,不需要诊断。”?请提交错误报告并查看正式答案。@myWallJSON:我不能:这不是错误。请查看我编辑的答案。
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'
#include <iostream>
struct A { A g(int) { return A(); } };
struct B { B g() { return B(); } };
struct C
{
template<typename... Ts>
auto f(Ts... ts) -> decltype(A().g(ts...))
{ std::cout << "f -> A" << std::endl; return A(); }
template<typename... Ts>
auto f(Ts... ts) -> decltype(B().g(ts...))
{ std::cout << "f -> B" << std::endl; return B(); }
};
int main()
{
C c;
c.f(1);
}