【发布时间】:2016-07-16 14:24:42
【问题描述】:
我有一个模板函数,其模板参数数量可变,它们“中继”到 std::function 参数:
template<typename... Args>
void test1(const std::function<void(Args...)> &function)
{}
static void t(float) {}
int main(int argc,char *argv[])
{
test1<float>(nullptr);
test1<float>(&t);
test1<float>([](float f) -> void {
});
return EXIT_SUCCESS;
}
这编译并运行良好,但 Visual Studio 将 main 中 test1 中的 all 调用标记为红色,并带有以下错误消息:
template<class... Args> void test1(const std::function<void(Args...)> &function)
no instance of function template "test1" matches the argument list
argument types are: (lambda []void(float f)->void)
另一方面,这不会显示为错误:
template<typename... Args>
void test2(Args... a)
{}
int main(int argc,char *argv[])
{
test2<float>(1.f);
return EXIT_SUCCESS;
}
我在第一种情况下做错了什么还是误报?这只是 Visual Studio 本身的错误,编译器甚至不会抛出任何警告。
// 编辑:
我只是在 Linux 上用 g++-5 做了一些测试,它根本不让我编译代码:
root@******:/var/projects# g++-5 -std=c++1y test.cpp
test.cpp: In function ‘int main(int, char**)’:
test.cpp:12:22: error: no matching function for call to ‘test1(std::nullptr_t)’
test1<float>(nullptr);
^
test.cpp:5:7: note: candidate: template<class ... Args> void test1(const std::function<void(Args ...)>&)
void test1(const std::function<void(Args...)> &function)
^
test.cpp:5:7: note: template argument deduction/substitution failed:
test.cpp:12:22: note: mismatched types ‘const std::function<void(Args ...)>’ and ‘std::nullptr_t’
test1<float>(nullptr);
^
test.cpp:13:17: error: no matching function for call to ‘test1(void (*)(float))’
test1<float>(&t);
^
test.cpp:5:7: note: candidate: template<class ... Args> void test1(const std::function<void(Args ...)>&)
void test1(const std::function<void(Args...)> &function)
^
test.cpp:5:7: note: template argument deduction/substitution failed:
test.cpp:13:17: note: mismatched types ‘const std::function<void(Args ...)>’ and ‘void (*)(float)’
test1<float>(&t);
^
test.cpp:16:3: error: no matching function for call to ‘test1(main(int, char**)::<lambda(float)>)’
});
^
test.cpp:5:7: note: candidate: template<class ... Args> void test1(const std::function<void(Args ...)>&)
void test1(const std::function<void(Args...)> &function)
^
test.cpp:5:7: note: template argument deduction/substitution failed:
test.cpp:16:3: note: ‘main(int, char**)::<lambda(float)>’ is not derived from ‘const std::function<void(Args ...)>’
});
^
【问题讨论】:
标签: c++ visual-studio c++11 visual-studio-2015 variadic-templates