我知道lambda对象不是std::function对象,所以我知道这是行不通的:
\ntemplate <typename ...args_t>\nvoid func(std::function<void(args_t...)> function_, args_t ...args){\n /// do something here\n}\n\nvoid test() {\n func([](int a){ }, 1);\n}\nRun Code Online (Sandbox Code Playgroud)\n但如果我添加一个 type_identity 来包装它,为什么它会起作用呢?
\ntemplate <typename T>\nstruct type_identity {\n using type = T;\n};\ntemplate <typename T>\nusing type_identity_t = typename type_identity<T>::type;\n\ntemplate <typename... args_t>\nvoid func_wrapped(type_identity_t<std::function<void(args_t...)>> function_, \n args_t ...args) {\n static_assert(std::is_same< std::function<void(args_t...)>,\n type_identity_t<std::function<void(args_t...)>>\n >::value,\n "different type");\n /// do something here\n}\n\nvoid test() {\n func_wrapped([](int a){ }, 1);\n}\n\nRun Code Online (Sandbox Code Playgroud)\n据我所知,这两个看起来几乎相同,甚至通过了,这static_assert意味着它们与 .\n 相同std::is_same。\n但编译器并不这么认为。它告诉我,在前一个代码中,lambda 不能匹配任何函数,而后一个可以。
error: no matching …