我可以在头文件中有这样的定义吗?
constexpr double PI=3.14;
Run Code Online (Sandbox Code Playgroud)
在包含在几个cpp文件中的头文件中有这个问题吗?
我担心,因为它在标准中说这个constexpr有自己的内存,把它放在头文件中,并在几个cpp文件中添加标题,在内存中生成相同值的多个副本以及其他一些令人讨厌的问题.
我正在使用C++ 11
在下面的代码中,如果由于歧义而定义了多个强制转换运算符,我希望得到编译器错误.
#include <iostream>
#include <sstream>
struct A
{
operator const char*() { return "hello world\n"; }
operator float() { return 123.0F; }
//operator int() { return 49; }
};
int main()
{
A a;
std::stringstream ss;
ss << a;
std::cout << ss.str();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
相反,只要定义了一个数字强制转换运算符,它就会编译没有错误,没有警告,并且优先使用数字强制转换operator const char *().声明的运算符的顺序没有区别.
然而,如果operator int()和operator float()都定义,那么我得到了我从一开始就预计:
'operator <<'含糊不清
是否有强制转换的优先规则,或者为什么编译器默认选择数值转换?我明白我应该明确说明我的意思,但我的问题是关于编译器的默认选择.
以下代码在Visual Studio 2008中编译,但在Visual Studio 2013及更高版本中失败.
std::string str("foo");
std::stringstream ss(str);
float f = 0;
if ((ss >> f) == false)
std::cout << "Parse error\n";
Run Code Online (Sandbox Code Playgroud)
错误消息是
错误C2678:二进制'==':找不到哪个运算符带有'std :: basic_istream>'类型的左操作数(或者没有可接受的转换)
并通过以下更改成功修复:
if (!(ss >> f))
std::cout << "Parse error\n";
Run Code Online (Sandbox Code Playgroud)
我不太了解这一点.我的问题是,ios涉及哪些运算符或强制转换或可能是 标志,允许流读取首先被评估为布尔值,然后为什么缺少一个operator==中断呢?
我知道已经构造的bitset对象的set()函数,但我需要一个构造的bitset,所有位都是1.这种情况是一个默认的函数参数.例如:
void bar(std::bitset<100> flags = X) {
}
Run Code Online (Sandbox Code Playgroud)
X应该是什么,-1可能适用于前64位,但不是全部.
我怀疑C++编译器的解析器是否为Clang,编译器如何处理运算符>>以了解何时它是二元运算符以及何时关闭模板如:std::vector<std::tuple<int, double>>我想这是在解析器时间内完成的,所以更好的方法是解决那个词汇或仅使用>作为标记,并解决语法分析器中的问题?
我试图找到一种方法来调用许多类成员函数,每个函数都有不同的参数,在调用之前和之后发生某些已知的功能.
这个包装函数是我尝试过的,但是例如对它的最后调用不会编译错误:
'bool Wrapper(Work*,std :: function <bool(Args ...)>,Args && ...)':无法推断'std :: function <bool的模板参数(double,std :: string, Args ...)>'from'std :: _ Bind <true,bool,std :: _ Pmf_wrap <bool(__ thiscall Work ::*)(double,std :: string),bool,Work,double,std :: string >,Work*const>'
class Work
{
public:
void DoWork(int a, double b, string c);
private:
void Pre() {};
void Post() {};
bool Step1() { return true; }
bool Step2(int) { return true; }
bool Step3(double, string) { return true; }
};
template<typename... Args>
bool Wrapper(Work *work, std::function<bool(Args...)> func, Args&&... args) …Run Code Online (Sandbox Code Playgroud) 我正在使用C++ 11以这种方式复制文件:
std::ifstream src(srcPath, std::ios::binary);
std::ofstream dst(destinationPath, std::ios::binary);
dst << src.rdbuf();
Run Code Online (Sandbox Code Playgroud)
我这样创建一个新文件:
std::ofstream out(path);
out << fileContent;
out.close();
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,如何检查操作是否实际成功或是否失败?
我有与此类似的C ++ 14代码:
void C::f() {
int& ref = this->x;
auto lb = [&ref]() {
/* do stuff with "ref" */
};
if (foobar) {
// call lb when signal fires.
connect(object, &D::signal, [&lb]() {
lb();
});
} else {
lb();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道到我使用时lb,它this仍然有效。但是ref和lb。上面的代码有悬挂的参考吗?
我已经在类中描述了交换函数,据我了解,如果类定义了它自己的交换函数,则它应该优先于内置交换函数。
以下两行来自C ++入门,接着出现一个问题,询问您使用类上算法标头中的排序算法,并观察调用交换的次数。
“如果一个类定义了自己的交换,那么该算法将使用该类的特定版本。否则,它将使用该库定义的交换函数。”
class HasPtr{
public:
friend void swap(HasPtr&, HasPtr&);
friend bool operator<(const HasPtr&,const HasPtr&);
HasPtr(const std::string& s=std::string()) : ps(new std::string(s)), i(0){}
private:
std::string *ps;
int i;
};
inline void swap(HasPtr&lhs, HasPtr& rhs){
using std::swap;
swap(lhs.ps,rhs.ps);
swap(lhs.i, rhs.i);
std::cout<<"Swap was called"<<std::endl;
}
bool operator<(const HasPtr& lhs,const HasPtr& rhs){
return *lhs.ps<*rhs.ps;
}
int main(){
std::vector<HasPtr>v1{HasPtr("this"),HasPtr("is"),HasPtr("a"),HasPtr("keyboard")};
std::sort(v1.begin(),v1.end());}
Run Code Online (Sandbox Code Playgroud)
现在,由于对sort的调用使用了<来重新排列元素,并且根据摘录,算法应使用类定义的swap版本,因此,每次算法重新排列元素时都应打印“ swap was被调用”,但似乎并没有并且简单地出现使用默认版本的swap是std :: swap。
第一:
LPCTSTR asdfsdf = (LPCTSTR)(_bstr_t)v;
printf("%s\n", asdfsdf);
Run Code Online (Sandbox Code Playgroud)
第二:
printf("%s\n", (LPCTSTR)(_bstr_t)v);
Run Code Online (Sandbox Code Playgroud)
它们是相同的,但第一个条件导致无法读取的代码
为什么?