c++03

Are static class members guaranteed to be initialized before `main` is called?

最后都变了- 提交于 2020-06-24 04:47:59
问题 Is there any guarantee that static class members are initialized before main is called? 回答1: I think no : [C++03: 3.6.2/3]: It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main . If the initialization is deferred to some point in time after the first statement of main , it shall occur before the first use of any function or object defined in the same translation unit as the

How to fill data into container at once without temp variable in C++03

独自空忆成欢 提交于 2020-05-29 09:59:26
问题 Suppose I have a Container . template<typename Type> class Container { public: Container(int size_) { size=size_; data = new Type[size]; } ~Container() { delete [] data; } private: int size; Type* data; }; I want something fill data into container at once like this Container<int> container(3); container << 100,200,300; or Container<int> container(3); container.fill({100,200,300}); or Container<int> container{100,200,300}; after do this, data[0]=100 , data[1]=200 , data[2]=300 I do NOT want

Does “&s[0]” point to contiguous characters in a std::string?

依然范特西╮ 提交于 2020-01-18 03:24:04
问题 I'm doing some maintenance work and ran across something like the following: 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 ); I know using &s[0] would be safe if it was a std::vector, but is this a safe use of std::string? 回答1: A std::string's allocation is not guaranteed to be contiguous under the C++98/03 standard, but C++11 forces it to be. In practice, neither I nor Herb Sutter know of an implementation

Default initialization of POD vs. non-POD class types

人走茶凉 提交于 2020-01-14 10:15:08
问题 The C++ standard says (8.5/5): To default-initialize an object of type T means: If T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor). If T is an array type, each element is default-initialized. Otherwise, the object is zero-initialized. With this code struct Int { int i; }; int main() { Int a; } the object a is default-initialized, but clearly a.i is not necessarily equal to 0 . Doesn

How can I search a container of objects for a data member value?

浪尽此生 提交于 2020-01-13 17:03:10
问题 I have an object type like this: struct T { int x; bool y; }; and a container of them like this: std::vector<T> v; and a burning desire to determine — in a single statement — whether any of the elements of v have y == true . This likely involves std::find_if . My understanding is that std::bind and boost::bind are for member functions and cannot be applied to member data. Because I dislike them, I wish to avoid: comparison functions/functors loops Because my environment is C++03, the

Perfect Forwarding in C++03

我与影子孤独终老i 提交于 2020-01-10 17:13:54
问题 If you have this function template<typename T> f(T&); And then try to call it with, let's say an rvalue like f(1); Why isn't T just be deduced to be const int, making the argument a const int& and thus bindable to an rvalue? 回答1: This is mentioned as a potential solution in the document I linked in the recent C++0x forwarding question. It would work fairly well, but it breaks existing code. Consider (straight from the document): template<class A1> void f(A1 & a1) { std::cout << 1 << std::endl

Is `double` guaranteed by C++03 to represent small integers exactly?

妖精的绣舞 提交于 2020-01-10 04:55:11
问题 Does the C++03 standard guarantee that sufficiently small non-zero integers are represented exactly in double ? If not, what about C++11? Note, I am not assuming IEEE compliance here. I suspect that the answer is no , but I would love to be proved wrong. When I say sufficiently small , I mean, bounded by some value that can be derived from the guarantees of C++03, and maybe even be calculated from values made available via std::numeric_limits<double> . EDIT: It is clear (now that I have

C++98/03 std::is_constructible implementation

旧巷老猫 提交于 2020-01-10 03:53:05
问题 The base components of my hobby library has to work with C++98 and C++11 compilers. To learn and to enjoy myself I created the C++98 implementations of several type support functionality (like enable_if , conditional , is_same , is_integral etc. ...) in order to use them when there is no C++11 support. However while I was implementing is_constructible I got stuck. Is there any kind of template magic (some kind of SFINAE) with which I can implement it without C++11 support ( declval )? Of

Finding a Pointer to NULL

拜拜、爱过 提交于 2020-01-05 04:33:07
问题 I have int* foo[SIZE] and I want to search it for the first element that points to NULL . But when I do this: std::find(foo, foo + SIZE, NULL) I get the error: error C2446: '==' : no conversion from 'const int' to 'int *' Should I just be using static_cast<int*>(NULL) instead of NULL ? C++11 solves this via nullptr but that's not an option for me in C++03 回答1: tl;dr: Use nullptr , or define your own equivalent. The problem is that NULL is some macro that expands to an integral constant

C++: Supplying a templated compare function to std::sort

大城市里の小女人 提交于 2020-01-03 17:10:16
问题 Suppose I want to get std::sort to sort a vector of pointers to int's, based on the value of the int's the pointers point to. Ignore the obvious performance issue there. Simple huh? Make a function: bool sort_helper(const int *a, const int *b) { return *a < *b; } and supply to the std::sort. Now, if we also want to do the same thing with a vector of pointers to large objects. The same thing applies: first we define a < operator in the object, then make a function along the following lines: