众所周知,C++中的内置枚举不是类型安全的.我想知道实现类型安全枚举的哪些类在那里使用...我自己使用以下"自行车",但它有点冗长和有限:
typesafeenum.h:
struct TypesafeEnum
{
// Construction:
public:
TypesafeEnum(): id (next_id++), name("") {}
TypesafeEnum(const std::string& n): id(next_id++), name(n) {}
// Operations:
public:
bool operator == (const TypesafeEnum& right) const;
bool operator != (const TypesafeEnum& right) const;
bool operator < (const TypesafeEnum& right) const;
std::string to_string() const { return name; }
// Implementation:
private:
static int next_id;
int id;
std::string name;
};
Run Code Online (Sandbox Code Playgroud)
typesafeenum.cpp:
int TypesafeEnum::next_id = 1;
bool TypesafeEnum::operator== (const TypesafeEnum& right) const
{ return id == right.id; }
bool TypesafeEnum::operator!= …Run Code Online (Sandbox Code Playgroud) 我正在做一些维护工作,遇到类似以下的事情:
std::string s;
s.resize( strLength );
// strLength is a size_t with the length of a C string in it.
memcpy( &s[0], str, strLength );
Run Code Online (Sandbox Code Playgroud)
我知道使用&s [0]如果它是std :: vector会是安全的,但是这是std :: string的安全使用吗?
C++ 03中的并发内存模型是什么?
(而且,C++ 11是否会更改内存模型以更好地支持并发性?)
我已经查看了一系列有关序列点的问题,并且无法x*f(x)确定f修改后的评估顺序是否有保证x,这是不同的f(x)*x.
考虑以下代码:
#include <iostream>
int fx(int &x) {
x = x + 1;
return x;
}
int f1(int &x) {
return fx(x)*x; // Line A
}
int f2(int &x) {
return x*fx(x); // Line B
}
int main(void) {
int a = 6, b = 6;
std::cout << f1(a) << " " << f2(b) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这打印49 42在g ++ 4.8.4(Ubuntu 14.04)上.
我想知道这是保证行为还是未指明.
具体来说,在这个程序中,fx两次x=6都被调用两次,并且两次都返回7次.所不同的是线A计算7×7(取的值x之后 …
我知道在C++ 03中,从技术上讲,std::basic_string模板不需要具有连续的内存.但是,我很好奇有多少实现存在于实际利用这种自由的现代编译器.例如,如果想要用来basic_string接收某些C API的结果(如下面的例子),分配一个向量只是为了立即将它变成一个字符串似乎很愚蠢.
例:
DWORD valueLength = 0;
DWORD type;
LONG errorCheck = RegQueryValueExW(
hWin32,
value.c_str(),
NULL,
&type,
NULL,
&valueLength);
if (errorCheck != ERROR_SUCCESS)
WindowsApiException::Throw(errorCheck);
else if (valueLength == 0)
return std::wstring();
std::wstring buffer;
do
{
buffer.resize(valueLength/sizeof(wchar_t));
errorCheck = RegQueryValueExW(
hWin32,
value.c_str(),
NULL,
&type,
&buffer[0],
&valueLength);
} while (errorCheck == ERROR_MORE_DATA);
if (errorCheck != ERROR_SUCCESS)
WindowsApiException::Throw(errorCheck);
return buffer;
Run Code Online (Sandbox Code Playgroud)
我知道像这样的代码可能会略微降低可移植性,因为它意味着它std::wstring是连续的 - 但我想知道这个代码是多么不可移植.换句话说,编译器如何实际利用具有非连续内存的自由?
编辑:我更新了这个问题,提到C++ 03.读者应注意,在定位C++ 11时,标准现在要求basic_string是连续的,因此在定位该标准时,上述问题不是问题.
在C++中,以下是否有未定义的行为:
int i = 0;
(i+=10)+=10;
Run Code Online (Sandbox Code Playgroud)
在我对C和C++中+ =的结果的答案的评论中有一些争论?这里的微妙之处在于默认响应似乎是"是",而正确的答案似乎是"它取决于C++标准的版本".
如果它确实取决于标准的版本,请解释UB的位置和不在的位置.
我已经在定义为的头文件中构建了一个使用常量的工作C库
typedef struct Y {
union {
struct bit_field bits;
uint8_t raw[4];
} X;
} CardInfo;
static const CardInfo Y_CONSTANT = { .raw = {0, 0, 0, 0 } };
Run Code Online (Sandbox Code Playgroud)
我知道.raw初始化程序只是C语法.
如何在其中使用联合定义常量,以便我可以在C和C++中使用它们.
以前这是std::string::c_str()工作,但是从C ++ 11开始,data()它也提供了工作,为什么还要c_str()添加null终止字符std::string::data()?在我看来,这似乎是在浪费CPU周期,如果null终止字符根本不相关而仅data()使用,C ++ 03编译器就不必关心终止符,也不必每次调整字符串大小时,都必须向终止符写入0,但是由于data()-null-guarantee,C ++ 11编译器不得不浪费每次写入字符串大小时都要写入0的周期,因此,这可能会使代码变慢,我想他们有一定理由要添加该保证,那是什么?
一位同事告诉我,在以下类型中,所有成员在内存中都有未指定的顺序(相对于彼此).
我对此表示怀疑,因为它们都具有相同的访问级别.
谁是对的?
struct foo { public: int x; public: int y; public: int z; };
Run Code Online (Sandbox Code Playgroud) 在C++ 参考的c_str()在std::string出现以下:
返回值
指向基础字符存储的指针.
data()[i] == operator[](i) for every i in [0, size())(直到C++ 11)
data() + i == &operator[](i) for every i in [0, size()](自C++ 11起)
我不明白两者之间的区别,除了自C++ 11以来一个元素的范围增加.
前一种说法data()[i] == operator[](i)对后者来说也不正确吗?
c++03 ×10
c++ ×9
c++11 ×4
string ×3
c ×2
stdstring ×2
c-str ×1
concurrency ×1
enumeration ×1
enums ×1
memcpy ×1
memory-model ×1
stl ×1
type-safety ×1
unions ×1
winapi ×1