我有一个与模板函数和线程有关的问题:
template <class TYPE_size>
void Threader(TYPE_size counter)
{
counter++;
}
int main()
{
unsigned int counter = 100;
thread one(Threader,counter);
one.join();
cout << counter;
}
Run Code Online (Sandbox Code Playgroud)
这不编译; 我明白了:
错误:没有匹配函数来调用âstd:: thread :: thread(,unsigned int&)â
如果我删除它编译的模板,如果我将函数调用更改为标准函数调用而不是线程(仍使用模板),则编译.
有人知道为什么吗?
我正在使用Centos5 64位.
error: no matching function for call to âstd::thread::thread(<unresolved overloaded function type>, unsigned int&)â
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:124: note: candidates are: std::thread::thread(std::thread&&)
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:122: note: std::thread::thread(const std::thread&)
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:121: note: std::thread::thread()
Run Code Online (Sandbox Code Playgroud) 我正在学习C++编程语言,在一章中我的书向我介绍了常量的概念:
必须为constexpr符号常量指定在编译时已知的值
在编译时知道什么值?我们为什么需要它们?
假设我们有一个名为datasize 的数组5.如果我们将此数组作为参数传递给std::end函数
int *ptr = std::end(data);
Run Code Online (Sandbox Code Playgroud)
它将返回指向数组中最后一个元素的内存位置的指针.
题
指针指向一个超过数组中最后一个元素的内存位置有什么意义?为什么不指向数组中的最后一个元素?
考虑以下代码
int i;
class A
{
public:
~A()
{
i=10;
}
};
int foo()
{
i=3;
A ob;
return i;
}
int main()
{
cout << foo() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
既然i是全球性的,我认为这个程序的输出应该是10.ob,当它超出范围会调用应设置的值的析构函数i来10.
示例代码如下:
class A
{
public:
int k;
virtual int f();
};
class B:public virtual A
{
public:
virtual int a();
};
int main()
{
cout<<sizeof(A)<<sizeof(B);
}
Run Code Online (Sandbox Code Playgroud)
它打印
8 12
它似乎class B有自己的新虚函数表.
如果class A更改为:
class A
{
public:
virtual int f();
};
Run Code Online (Sandbox Code Playgroud)
它打印
4 4
谁有人解释原因?
我试图用std::set一组unique_ptr来定义我定义的自定义对象.我在定义集合时提供自定义比较功能(以启用深度比较).在将元素插入集合时,此比较函数似乎正常工作,即具有相同内容的项目未插入两次.
但是,如果我使用比较两个集合operator==,它似乎被忽略,即具有等效元素的集合返回不相等,而我期望(希望)它是相等的(因为我提供的自定义比较函数做了深度比较) .
比较功能是否仅在插入期间使用?如果是这样,是否可以选择进行operator==深入比较?
任何指针赞赏.谢谢 :)
示例代码
//
// main.cpp
// Test
#include <iostream>
#include <set>
class Person {
private:
std::string mName;
public:
Person(const std::string& name);
virtual ~Person() {}
void setName(std::string& name);
std::string getName();
};
typedef std::unique_ptr<Person> PersonUniquePtr;
Person::Person(const std::string& name)
: mName{ name }
{
}
void Person::setName(std::string& name)
{
mName = name;
}
std::string Person::getName()
{
return mName;
}
bool isLess(Person* p1, Person* p2)
{
if (p1->getName().compare(p2->getName()) == -1)
return …Run Code Online (Sandbox Code Playgroud) 我只有一个用于C++学校作业的hpp文件(我不允许添加cpp文件,声明和实现都应该写在文件中).
我在里面写了这段代码:
template<class T>
class Matrix
{
void foo()
{
//do something for a T variable.
}
};
Run Code Online (Sandbox Code Playgroud)
我想添加另一种foo方法,但这foo()将只适用于<int>.我在某些地方读过,我需要声明一个新的专门化类来实现它.但我想要的是专门的foo将位于原始的下方foo,所以它看起来像这样:
template<class T>
class Matrix
{
void foo(T x)
{
//do something for a T variable.
}
template<> void foo<int>(int x)
{
//do something for an int variable.
}
};
Run Code Online (Sandbox Code Playgroud)
谢谢
你会如何转换std::string成BSTR*?
STDMETHODIMP CMyRESTApp::rest(BSTR data, BSTR* restr)
{
RESTClient restclient;
RESTClient::response resp = restclient.get(data);
Log("Response Status code: %s", resp.code);
Log("Response Body: %s", resp.body);
*restr = // here
return S_OK;
}
Run Code Online (Sandbox Code Playgroud)
我需要转换resp.body,然后返回*restr此处.
下面的代码给出了错误:
use of deleted function ‘constexpr B::B(const B&)’
Run Code Online (Sandbox Code Playgroud)
现在,我知道这是因为通过指定移动构造函数(有意)隐式删除了复制构造函数,并且复制向量会导致对(已删除)复制构造函数的调用.我想我也理解为什么使用向量的复制构造函数和赋值运算符.我显然想要使用移动构造函数和赋值运算符:移动对象,因此也移动它包含的向量.那么,如何让我的移动构造函数/赋值运算符使用向量的移动构造函数/赋值运算符?
这是代码:
#include <vector>
class B {
private:
/* something I don't want to copy */
public:
B() {};
B(B&& orig) {/* move contents */};
B& operator=(B&& rhs) {
/* move contents */
return *this;
};
};
class A {
private:
vector<B> vec;
public:
A() : vec() {};
A(A&& orig) : vec(orig.vec) {};
A& operator=(A&& rhs) {
vec = rhs.vec;
return *this;
};
};
Run Code Online (Sandbox Code Playgroud)