假设我有一个
template <typename T>
class A :
class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>,
anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T> { ... }
Run Code Online (Sandbox Code Playgroud)
现在,A类的定义,和/或它的方法,我需要参考两个超类(例如,要在超成员访问,或者在它等定义的类型),但我想避免重复超的名字.目前,我正在做的是:
template<typename T>
class A :
class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>,
anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T>
{
using parent1 = class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>;
using parent2 = anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T>;
...
}
Run Code Online (Sandbox Code Playgroud)
显然,这有效,并将重复次数减少到2; 但如果可能的话,我宁愿避免这种重复.有合理的方法吗?
笔记:
这里有一些代码可以过滤掉满足谓词的地图元素到新地图(MCVE-fied):
#include <algorithm>
#include <unordered_map>
#include <iostream>
using namespace std;
int main() {
unordered_map<string, int> m = { { "hello", 1 }, { "world", 2 } };
auto p = [](const decltype(m)::value_type& e) { return e.second == 2; };
const auto& m2(m);
auto m3(m2);
auto it = remove_if(m3.begin(), m3.end(), p);
m3.erase(it, m3.end());
cout << "m3.size() = " << m3.size() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
remove_if()行编译失败,我得到:
In file included from /usr/include/c++/4.9/utility:70:0,
from /usr/include/c++/4.9/algorithm:60,
from /tmp/b.cpp:1:
/usr/include/c++/4.9/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>& …Run Code Online (Sandbox Code Playgroud) 我想使用全局constexpr变量:
constexpr int foo = 123;
Run Code Online (Sandbox Code Playgroud)
而不是C宏:
#define FOO (123)
Run Code Online (Sandbox Code Playgroud)
在我写的一些代码中.我希望保证相同的行为,从某种意义上说,它不会在运行时占用内存空间,也不会在编译的目标代码中可见/存在(也就是说,它的值将被用作直接的地方相关).
我可以得到这个保证吗?在某些条件下?当然,假设我不是想使用x的地址或任何这样有趣的业务.
是否可以在不阻塞主机的情况下同步两个CUDA流?我知道有cudaStreamWaitEvent,它是非阻塞的。但是使用cudaEventCreate
和创建和破坏事件呢cudaEventDestroy?
该文档的cudaEventDestroy说:
如果调用cudaEventDestroy()时已记录事件但尚未完成事件,则该函数将立即返回,并且一旦设备完成事件,与事件关联的资源将自动释放。
我不明白的是,已记录事件和已完成事件之间有什么区别。同样,这似乎意味着如果尚未记录该事件,则呼叫正在阻塞。
任何人都可以对此有所了解吗?
是否有任何根本原因新的C++ 17(或更高版本)将不允许写的另一种方式main为
int main(std::vector<std::string> args){...}
Run Code Online (Sandbox Code Playgroud)
?我知道需要与以前的代码兼容,所以
int main(int, char**)
Run Code Online (Sandbox Code Playgroud)
仍然必须存在,但是有什么技术可以阻止第一个"替代"声明吗?
为什么这个有效的C++不是?:
enum foo : unsigned { first_foo, second_foo };
enum bar : foo { best_foo = first_foo };
Run Code Online (Sandbox Code Playgroud)
GCC 5.4.0说:
/tmp/a.cpp:3:16: error: underlying type ‘foo’ of ‘bar’ must be an integral type
enum bar : foo { best_foo = first_foo };
Run Code Online (Sandbox Code Playgroud)
我可以理解为什么我会得到这个错误,如果foo是一个float,或一些结构,或什么不是.但就语义,类型安全等而言,这似乎是完全合法的.我缺少什么?
我最近习惯了水银后开始使用git。
从本质上讲,如果我hg add有一些文件,那么hg diff我会得到一个补丁,然后可以从理论上应用一个简单的补丁,patch -p1并获得完全相同的本地副本。
现在,有了git,情况就不同了:在您git diff之前git add。但是,我该如何git diff覆盖ing hg diff之后的所有未跟踪文件hg add呢?
我有一个格式正确的CSV文件,可能有也可能没有标题行; 并且可能有也可能没有引用数据.我想使用shell确定其中的列数.
现在,如果我可以确定文件中没有带引号的逗号,则以下内容似乎有效:
x=$(tail -1 00-45-19-tester-trace.csv | grep -o , | wc -l); echo $((x + 1))
Run Code Online (Sandbox Code Playgroud)
但如果我不能做出这样的假设呢?也就是说,如果我不能假设逗号总是一个字段分隔符怎么办?那我该怎么办?
如果它有帮助,你可以假设没有引用的引号(即\"在引用的字符串之间的s); 但最好不要制作那个.
我想写下面的CUDA函数:
void foo(int* a, size_t n)
{
if ( /* MAGIC 1 */ ) {
// a is known to be in shared memory,
// so use it directly
}
else {
// make a copy of a in shared memory
// and use the copy
}
}
Run Code Online (Sandbox Code Playgroud)
在主机端,我们有一个与cudaPointerGetAttributes形式有关的设施,可以告诉我们指针是指设备内存还是主机内存; 也许有一些方法可以区分设备代码中的指针,也许它也可以从全局指针中辨别共享.或者,也许甚至更好 - 也许有一个编译时机制来做到这一点,因为毕竟设备功能只被编译到内核中并且不是独立的,所以nvcc通常可以知道它们是否与共享内存一起使用.
我有一个std::unique_ptr<Foo>对象矢量.我想获得符合某些条件的所有矢量项的集合.我看到std函数,但它们似乎都测试谓词(并返回bool)或返回单个元素.
是否有内置机制来获取作为向量子集的集合?如果没有,有没有办法构建一个迭代器来测试项目对任意谓词(以识别符合我的条件的那些)和一种机制来返回满足该谓词的所有项目?
c++ ×6
c++11 ×3
cuda ×2
bash ×1
c ×1
const ×1
constexpr ×1
counting ×1
csv ×1
cuda-events ×1
dictionary ×1
diff ×1
enums ×1
git ×1
gpu ×1
idiomatic ×1
idioms ×1
inheritance ×1
iteration ×1
macros ×1
mercurial ×1
predicate ×1
remove-if ×1
shell ×1
std-pair ×1
stdvector ×1
subclassing ×1
unique-ptr ×1