std

__nat class in clang standard libarry

喜欢而已 提交于 2019-12-08 02:03:51
问题 I was looking through clang's c++ standard library, and found this class in the shared_ptr class. class shared_ptr ... private: struct __nat {int __for_bool_;}; ... }; and I understand that this class is used to detect whether type conversion is possible at compile time, but its member __for_bool_ is never used anywhere in the class or the weak_ptr counterpart. So my question is, what is the point of __for_bool_ ? Why not simply use an empty class for the same purpose? I'm sure the standard

std::string getting (char *) instead of (const char *)

一世执手 提交于 2019-12-08 00:59:18
问题 std::string.c_str() returns a (const char *) value. I Googled and found that I can do the following: std::string myString = "Hello World"; char *buf = &myString[0]; How is this possible? &myString[0] is an object of type std::string , so how can this work? 回答1: &myString[0] is a object of type std::string No it isn't. myString[0] is a reference to the first character of the string; &myString[0] is a pointer to that character. The operator precedence is such that it means &(myString[0]) and

How to understand C++ std::setw 's inconsistent behaviour?

旧城冷巷雨未停 提交于 2019-12-07 20:10:56
问题 Given the following code: /*Formatting Output **Goal: practice using cout to format output to console **Print the variables in three columns: **Ints, Floats, Doubles */ #include <iostream> #include <iomanip> using namespace std; int main() { int a = 45; float b = 45.323; double c = 45.5468; int aa = a + 9; float bb = b + 9; double cc = c + 9; int aaa = aa + 9; float bbb = bb + 9; double ccc = cc + 9; // 1st attempt :> cout << "\n\n\n" << "// 1st attempt :>" << "\n"; cout <<

How does a C++ std::container (vector) store its internals (element address, access by index)?

青春壹個敷衍的年華 提交于 2019-12-07 19:39:26
问题 I am trying to "hack" a game (Red Alert 3), I try to make a program which shows the unit list of my opponents. As for that I first need to find a (static) pointer to my own list which I can do on single player. I have noticed this behaviour: (by looking at which addresses are changed by the add_unit code): if a units hasn't been build yet, create a new address for it (random?) and set the value to 1 (amount of units of that type) when the unit has been already build once in the game,

Why doesn't std::string take a null pointer?

我是研究僧i 提交于 2019-12-07 18:16:08
问题 I recently passed a null pointer to a std::string constructor and got undefined behavior. I'm certain this is something that thousands or tens of thousands of programmers have done before me, and this same bug has no doubt crashed untold numbers of programs. It comes up a lot when converting from code using char* to code using std::string , and it's the kind of thing that is not catchable at compile time and can easily be missed in run time unit tests. What I'm confused about is the reason

std::vector size in header

这一生的挚爱 提交于 2019-12-07 17:18:28
I have small question about std::vector. In main.h i try to make fixed size int vector std::vector<int> foo(7); But g++ gived this error: ../test/main.h:21:26: error: expected identifier before numeric constant std::vector<int> foo(7); ../main/main.h:21:26: error: expected ',' or '...' before numeric constant How can i create private vector variable of fixed size length? Or should i simply make in constructor for(int i=0; i<7;i++){ foo.push_back(0); } Assuming foo is a data member, your syntax is invalid. In general, you can initialize a data member of type T like this: T foo{ctor_args}; or

Accept only letters

ε祈祈猫儿з 提交于 2019-12-07 16:31:00
问题 This should accept only letters, but it is not yet correct: #include <iostream> #include <string> #include <sstream> using namespace std; int main() { std::string line; double d; while (std::getline(std::cin, line)) { std::stringstream ss(line); if (ss >> d == false && line != "") //false because can convert to double { std::cout << "its characters!" << std::endl; break; } std::cout << "Error!" << std::endl; } return 0; } Here is the output: 567 Error! Error! 678fgh Error! 567fgh678 Error!

How to abstract lazy initialization in C++?

早过忘川 提交于 2019-12-07 09:00:49
问题 While refactoring some code for performance the other day, I needed an answer to creating member variables that are lazy initialized, but that also provides a convenient, though optional, non-lambda interface for non c++11 compilers. Here's the typical pattern for lazy instantiation that I want to abstract: if( !bInitialized ) { value = doInitialization(); bInitialized = true; } return value; For my use, I'd like some flexibility: allow explicit initialization, like the example above provide

Convert values to values inside a range in c++ , optimized using boost or std

ⅰ亾dé卋堺 提交于 2019-12-07 08:14:24
问题 I want to validate all the elemnent of an array. If an element is under a value, swap by a min value and if it is above a value, swap by a max value. But I don´t know how I can do it optimized. For do it I go above all elements, element by element but it is not optimized, and it spend a lot of cpu time in very large arrays. This is an example of my code: #include <iostream> #include <math.h> const int MAX = 10; int main () { float minVal = 2.0; float maxVal = 11.0; float vElem[] = {-111111.0

std::string and multiple concatenations

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 07:08:56
问题 Let’s consider that snippet, and please suppose that a, b, c and d are non-empty strings. std::string a, b, c, d; d = a + b + c; When computing the sum of those 3 std::string instances, the standard library implementations create a first temporary std::string object, copy in its internal buffer the concatenated buffers of a and b , then perform the same operations between the temporary string and the c . A fellow programmer was stressing that instead of this behaviour, operator+(std::string,