假设我有一个模板函数和两个类
class animal {
}
class person {
}
template<class T>
void foo() {
if (T is animal) {
kill();
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么检查T是动物?我不想在运行时检查一些东西.谢谢
我知道golang支持多个赋值,例如,
a, b = c, d
Run Code Online (Sandbox Code Playgroud)
我想知道左 - >右命令后的作业是否合适?例如,如果我玩树木:
parent, child = child, child.child
Run Code Online (Sandbox Code Playgroud)
它是否保证父和子都在树中更深一层?
真正的用例是什么?
std::integral_constant
Run Code Online (Sandbox Code Playgroud)
我可以理解这是一个具有值2的包装器:
typedef std::integral_constant<int, 2> two_t
Run Code Online (Sandbox Code Playgroud)
但是为什么不只使用2或用2定义const int值呢?
我在fmt.Sprintf中有一个很长的行.如何在代码中拆分它?我不想把所有东西放在一行,所以代码看起来很难看.
fmt.Sprintf("a:%s, b:%s ...... this goes really long")
Run Code Online (Sandbox Code Playgroud) 我有一个返回向量或集合的函数:
set<int> foo() {
set<int> bar;
// create and massage bar
return bar;
}
set<int> afoo = foo();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我在函数foo()中创建一个临时内存空间,然后afoo通过复制将其分配给它.我真的想避免这个副本,我可以用C++ 11做到这一点吗?我认为这与rvalue有关.
好的,更新问题:如果我要返回自己定义的对象,而不是向量或设置的东西,这是否意味着我应该定义一个移动构造函数?像这样:
class value_to_return {
value_to_return (value_to_return && other) {
// how to write it here? I think std::move is supposed to be used?
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!!!
我正在努力学习如何使用原子:)
class foo {
static std::atomic<uint32_t> count_;
uint32 increase_and_get() {
uint32 t = count_++;
return t;
}
}
Run Code Online (Sandbox Code Playgroud)
功能是increase_and_get()线程安全的吗?
我有一个人类使用通用参考ctor
class Human {
public:
template<typename T>
explicit Human(T&& rhs) {
// do some initialization work
}
Human(const Human& rhs); // the default ctor I don't care about
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我有一个const Human对象
const Human one_I_do_not_care; // then play with that
Human the_human_I_care(one_I_do_not_care) // now create another one
Run Code Online (Sandbox Code Playgroud)
最后一行是使用模板ctor还是默认的ctor?我的理解是"const"会取消模板ctor的资格,我是否正确?
Human the_human_I_care(one_I_do_not_care) // line in question
Run Code Online (Sandbox Code Playgroud)
通过const取消模板ctor的资格,我的意思是添加const然后它将不匹配模板ctor,不是它仍然匹配两个,但编译器选择一个.
我有一个模板功能
template <class T>
void foo() {
// Within this function I need to create a new T
// with some parameters. Now the problem is I don't
// know the number of parameters needed for T (could be
// 2 or 3 or 4)
auto p = new T(...);
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?不知怎的,我记得看到函数输入像(...,...)?
我有两个无符号向量,大小均为 4
vector<unsigned> v1 = {2, 4, 6, 8}
vector<unsigned> v2 = {1, 10, 11, 13}
Run Code Online (Sandbox Code Playgroud)
现在我想将这两个向量相乘并得到一个新向量
vector<unsigned> v_result = {2*1, 4*10, 6*11, 8*13}
Run Code Online (Sandbox Code Playgroud)
SSE操作要使用什么?是跨平台还是只在某些特定平台上?
添加:如果我的目标是加法而不是乘法,我可以超级快地完成此操作:
__m128i a = _mm_set_epi32(1,2,3,4);
__m128i b = _mm_set_epi32(1,2,3,4);
__m128i c;
c = _mm_add_epi32(a,b);
Run Code Online (Sandbox Code Playgroud) 我的go程序中定义了以下对
type pair struct {
a float64
b float64
}
Run Code Online (Sandbox Code Playgroud)
然后我创建一个地图:
dictionary map[string]pair
Run Code Online (Sandbox Code Playgroud)
我先添加一个元素:
dictionary["xxoo"] = pair{5.0, 2.0}
Run Code Online (Sandbox Code Playgroud)
然后我试着这样做:
dictionary["xxoo"].b = 5.0 // try to change from 2.0 to 5.0
Run Code Online (Sandbox Code Playgroud)
最后一行没有编译,它说"不能分配给它"
我想知道这是什么原因?