有人可能想要使用参数声明一个函数,并指定参数的默认值是类型的默认构造函数的结果:
void foo(a::really::long::type::name arg = a::really::long::type::name());
Run Code Online (Sandbox Code Playgroud)
是否有更好的语法,不涉及输入类型名称两次?就像是:
void foo(a::really::long::type::name arg = default);
Run Code Online (Sandbox Code Playgroud)
我意识到我可以typedef使用类型名称使其更漂亮,但我很好奇是否存在这样的语法.
我在实用程序库中实现了以下功能:
template <auto Value>
bool equals_constant(const decltype(Value)& value) { return Value == value; }
Run Code Online (Sandbox Code Playgroud)
它对于采用可调用谓词的其他模板函数很有用,如下所示:
template <auto IsValid>
struct Validator {
bool validate(int value) { return IsValid(value); }
};
// this validator requires its value to equal 42 (just for demo purposes)
Validator<equals_constant<42>> validator;
std::cout << validator.validate(41) << std::endl; // 0
std::cout << validator.validate(42) << std::endl; // 1
Run Code Online (Sandbox Code Playgroud)
我是否重新发明了轮子equals_constant()- STL 中已经有类似的东西了吗?(如果是这样,我在cppreference或 Google上找不到它。)谢谢。
如何在Visual Studio 2012中添加自定义"X中运行/调试"命令?
我有一个C#/ .NET应用程序,我们称之为"X",它支持可加载的插件.要编写插件,我在Visual Studio中创建一个新的C#类库项目,引用某个程序集,然后编写我的代码.要测试插件,我需要将已编译的插件DLL复制到特定的运行时目录,编写XML配置文件,然后运行X.我可以使用Post-Build事件和项目的Debug属性自动执行其中一些操作("启动外部"程序").
但我真正想要的是单击Debug然后单击自定义"Run in X"(或"Debug in X")命令,该命令调用某种插件或脚本来执行所有这些操作.该脚本将处理创建X配置文件,运行时目录,复制DLL,启动X应用程序以及将调试器附加到其中.
我愿意写一个Visual Studio插件来完成这个,但是不知道从哪里开始.
我有一个带有两个参数的模板:第一个是类型,第二个是带有参数的函数指针,其类型是第一个模板参数。这个 MCVE 有效:
void returnsVoid(int x) { }
template <typename T, void (*Func)(T)>
struct foo { void bar(T t) { Func(t); } };
int main(int, char *[]) {
foo<int, returnsVoid> a; // ok
}
Run Code Online (Sandbox Code Playgroud)
但是,当我将第二个模板参数的返回类型更改为auto(如本相关问题中所述)时,出现错误:
void returnsVoid(int x) { }
template <typename T, auto (*Func)(T)>
struct foo {
void bar(T t) { Func(t); }
};
int main(int, char *[]) {
foo<int, returnsVoid> a; // error: unable to deduce ‘auto (*)(T)’ from ‘returnsVoid’
// note: mismatched types …Run Code Online (Sandbox Code Playgroud) 在许多UNIX TCP实现中,提供了一个套接字选项TCP_CORK,允许调用者绕过Nagle算法并明确指定何时发送物理数据包.Windows(Winsock)中是否有相同的功能?
TCP_CORK(自Linux 2.2起)
如果设置,请不要发送部分帧.再次清除该选项时,将发送所有排队的部分帧.这对于在调用sendfile(2)之前预先添加标头或用于吞吐量优化非常有用.正如目前实施的那样,TCP_CORK的输出时间上限为200毫秒.如果达到此上限,则自动发送排队的数据.自Linux 2.5.71起,此选项只能与TCP_NODELAY结合使用.此选项不应用于可移植的代码中.
(我知道TCP_NODELAY,但这不是我需要的;我仍然希望在发送缓冲区中累积多次写入,然后在我准备好发送物理数据包时触发TCP堆栈.)
请考虑以下内容(它不会编译,但稍后会修复):
void foo(const int *n) { }
template <typename ...Args>
void bar(void (*func)(Args...), Args... args) { func(args...); }
int main(int argc, char *argv[])
{
int n = 42;
bar(foo, &n);
}
Run Code Online (Sandbox Code Playgroud)
模板函数bar()需要一个函数指针来调用,并将参数包传递给它。gcc 7.4.0诊断以下错误:
test.cpp:6:6: note: template argument deduction/substitution failed:
test.cpp:11:16: note: inconsistent parameter pack deduction with ‘const int*’ and ‘int*’
Run Code Online (Sandbox Code Playgroud)
很明显,类型推导规则不够宽松,无法const T*同时观察到const T*和被推导T*。好的。这很容易用演员表修复:
bar(foo, static_cast<const int *>(&n));
Run Code Online (Sandbox Code Playgroud)
但这是丑陋的。C ++ 17具有std::as_const()使它变得不那么丑陋的(&std::as_const(n))的功能,但是在我当前的项目中,我仅限于C ++ 14的sadface。 …
给定 a ,std::tuple<A, B, ...> fooC++14 中是否有任何通用(模板化)函数或技术来获取包含std::tuple<B, ...> bar除第一个元素之外的所有元素的新元组foo?或者,也许是 Boost 中的某些东西?
我已经使用参数包和一些模板元编程编写了一个辅助函数来执行此操作,但我很想扔掉所有这些东西!
这就是我目前正在做的事情。我定义了一个辅助函数unshift_tuple(),它返回一个元组,其中包含传递给函数的元组中除第一个元素之外的所有元素。的实现unshift_tuple()使用一个辅助函数unshift_tuple_with_indices(),该函数采用包含要提取的元组索引的参数包;帮助sequential_integer_list器类型用于使用模板元编程生成适当的索引列表参数包。丑陋的!
#include <tuple>
template <size_t... Integers>
struct integer_list {};
template <size_t N, size_t... Args>
struct sequential_integer_list : sequential_integer_list<N - 1, N - 1, Args...> {};
template <size_t... Args>
struct sequential_integer_list<0, Args...> { typedef integer_list<Args...> type; };
template <typename FirstElement, typename... Elements, size_t... Indices>
static std::tuple<Elements...> unshift_tuple_with_indices(
const std::tuple<FirstElement, Elements...>& tuple,
integer_list<Indices...> index_type)
{
return …Run Code Online (Sandbox Code Playgroud) 我试图在waveOut.play()上运行"await Task.Run".原因是我想要这样做是因为我需要在waveOut.play()完成后立即删除文件.但是,我无法删除已经在使用的东西,这就是为什么我需要使用await task.run,但这次似乎不起作用.这可能是重复但我无法找到任何理由为什么程序不等待它完成.
public async void PlayAudio(object sender, GenericEventArgs<Stream> args)
{
fileName = $"{ Guid.NewGuid() }.wav";
using (var file = File.OpenWrite(Path.Combine(Directory.GetCurrentDirectory(), fileName)))
{
args.EventData.CopyTo(file);
file.Flush();
}
WaveOut waveOut = new WaveOut();
WaveFileReader reader = new WaveFileReader(fileName);
waveOut.Init(reader);
try
{
await Task.Run(() =>
{
waveOut.Play();
});
}
finally
{
File.Delete(fileName);
}
}
Run Code Online (Sandbox Code Playgroud)
程序按字符串名称查找文件并播放它.await task.run在这里不起作用.它直接跳到最后一块.