我有一个带有两个参数的函数:
template <typename T1, typename T2>
void foo(T1 arg1, T2 arg2)
{ std::cout << arg1 << " + " << arg2 << '\n'; }
Run Code Online (Sandbox Code Playgroud)
一个可变参数的参数应该成对转发:
template <typename... Args>
void bar(Args&&... args) {
static_assert(sizeof...(Args) % 2 == 0);
( foo( std::forward<Args>(args), std::forward<Args>(args) ), ... );
// ^ Sends each argument twice, not in pairs
}
Run Code Online (Sandbox Code Playgroud)
我想bar(1,2,3,4)打电话foo(1,2)和foo(3,4)
有没有办法做到这一点 ?
采取以下代码:
template <class T>
void my_func() { T::some_method(); }
int main() {
std::cout << (noexcept(my_func<SomeClass>()) ? "noexcept" : "can throw") << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
my_func()即使SomeClass::some_method()标记为noexcept,这将始终打印出可能抛出的图像。(至少使用gcc 7.4.0和-std = c ++ 17)
有没有一种实用的方法可以使编译器noexcept根据模板参数检测函数是否存在?
我唯一想到的就是使用std :: enable_if:
template <class T>
std::enable_if_t<true == noexcept(T::some_method())>
my_func() noexcept { T::some_method(); }
template <class T>
std::enable_if_t<false == noexcept(T::some_method())>
my_func() { T::some_method(); }
Run Code Online (Sandbox Code Playgroud)
但是,这会占用大量空间并导致代码重复。
我想std::function从lambda创建一个,在 a 中template,它将所需的实例std::function作为其参数:
template <class functionT>
functionT make_lambda() {
return [](/* ignore all args */){ std::cout << "my lambda\n"; };
}
Run Code Online (Sandbox Code Playgroud)
然后调用template具有不同别名的std::function:
using function_no_args = std::function<void(void)>;
using function_args = std::function<void(int)>;
make_lambda<function_no_args>()(); // output: "my lambda"
make_lambda<function_args>()(999); // compile error
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
一些精度:
std::function,在traits结构中定义它们并将它们传递给我的代码的多个部分void,只有参数可能会改变