我一直在学习可变参数模板,在这篇优秀博客文章的帮助下,我设法编写了一个函数模板even_number_of_args,它返回它接收的参数数量是否可被2整除.
#include <iostream>
bool even_number_of_args() {
return true;
}
template <typename T>
bool even_number_of_args(T _) {
return false;
}
template<typename T, typename U, typename... Vs>
bool even_number_of_args(T _, U __, Vs... vs) {
return even_number_of_args(vs...);
}
int main() {
std::cout << even_number_of_args() << std::endl; // true
std::cout << even_number_of_args(1) << std::endl; // false
std::cout << even_number_of_args(1, "two") << std::endl; // true
std::cout << even_number_of_args(1, "two", 3.0) << std::endl; // false
std::cout << even_number_of_args(1, "two", 3.0, '4') …Run Code Online (Sandbox Code Playgroud) 我写了一个vibe.dweb-UI clang-format,当使用LLVM样式时显示此输入时,服务器挂起.
处理POST的代码:
void post(string style, string code)
{
import std.algorithm;
import std.file;
import std.conv;
import std.process;
auto pipes = pipeProcess(["clang-format", "-style="~style], Redirect.stdout | Redirect.stdin);
scope(exit) wait(pipes.pid);
pipes.stdin.write(code);
pipes.stdin.close;
pipes.pid.wait;
code = pipes.stdout.byLine.joiner.to!string;
string selectedStyle = style;
render!("index.dt", styles, code, selectedStyle);
}
Run Code Online (Sandbox Code Playgroud)
这可能不应该以阻塞的方式完成,但我不知道如何异步地执行它.我已经尝试包装函数的内容runTask,但我找不到正确调用它的方法.
我怎样才能让它可靠?
我有一个具有类似元组的界面的自定义类.因为我想我的代码尽可能通用的,我认为这将是我的基础上的功能的算法是一个好主意std::get,std::tuple_size,std::tuple_element所以你只需要专注这些功能用我的算法.让我们称之为需要这些功能特化的概念Tuple.
现在我试图总结一个组件Tuple.函数声明应该是这样的:
template <class Tuple>
int sum_components(const Tuple& t);
Run Code Online (Sandbox Code Playgroud)
我想有很多模板编程涉及但我无法弄清楚如何做到这一点.
对于添加,我只会使用全局的重载+ operator.
我正在使用c ++ 1z.