c++03

Does std::vector::resize() ever reallocate when new size is smaller than current size? [duplicate]

给你一囗甜甜゛ 提交于 2020-01-03 07:25:08
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: std::vector resize downward If I resize() an std::vector to some size smaller than its current size, is it possible that the vector will ever allocate new memory? This is important to me for performance reasons. 回答1: No, resize ing to a smaller size will never reallocate. In case the container shrinks, all iterators, pointers and references to elements that have not been removed remain valid after the resize and

Alternative to C++11's std::nextafter and std::nexttoward for C++03?

会有一股神秘感。 提交于 2020-01-02 07:10:11
问题 As the title says, the functionality I'm after is provided by C++11's math libraries to find the next floating point value towards a particular value. Aside from pulling the code out of the std library (which I may have to resort to), any alternatives to do this with C++03 (using GCC 4.4.6)? 回答1: Platform dependently, assuming IEEE754, and modulo endianness, you can store the data of the floating point number in an integer, increment by one, and retrieve the result: float input = 3.15; uint32

c++03: default constructor for build-in types in std::map

微笑、不失礼 提交于 2020-01-01 10:19:12
问题 I always thought that following code std::map<int, int> test; std::cout << test[0] << std::endl; would print random value, because it would create unitialized value within map. However, it turns out that created int is actually always initialized to zero AND standard builtin types are also zero-initialized in certain circumstances. The question is : when zero-initialziation is performed for standard types (int/char/float/double/size_t)? I'm pretty sure that if I declare int i; in the middle

Thread-safe static variables without mutexing?

邮差的信 提交于 2020-01-01 08:28:52
问题 I remember reading that static variables declared inside methods is not thread-safe. (See What about the Meyer's singleton? as mentioned by Todd Gardner) Dog* MyClass::BadMethod() { static Dog dog("Lassie"); return &dog; } My library generates C++ code for end-users to compile as part of their application. The code it generates needs to initialize static variables in a thread-safe cross-platform manner. I'd like to use boost::call_once to mutex the variable initialization but then end-users

Thread-safe static variables without mutexing?

只谈情不闲聊 提交于 2020-01-01 08:28:20
问题 I remember reading that static variables declared inside methods is not thread-safe. (See What about the Meyer's singleton? as mentioned by Todd Gardner) Dog* MyClass::BadMethod() { static Dog dog("Lassie"); return &dog; } My library generates C++ code for end-users to compile as part of their application. The code it generates needs to initialize static variables in a thread-safe cross-platform manner. I'd like to use boost::call_once to mutex the variable initialization but then end-users

Does *&++i cause undefined behaviour in C++03?

£可爱£侵袭症+ 提交于 2020-01-01 07:31:14
问题 In another answer it was stated that prior to C++11, where i is an int , then use of the expression: *&++i caused undefined behaviour. Is this true? On the other answer there was a little discussion in comments but it seems unconvincing. 回答1: It makes little sense to ask whether *&++i in itself has UB. The deferencing doesn't necessarily access the stored value (prior or new) of i , as you can see by using this as an initializer expression for a reference. Only if an rvalue conversion is

Function return type deduction in C++03

心已入冬 提交于 2020-01-01 07:01:11
问题 The tags ask the question, but nonetheless, consider the following: template<typename F, typename A, typename R> R call(F function, A arg) { return function(arg); } int foo(char) { return 5; } int main() { call(foo, 'a'); } The compiler happily compiles this if parameter R is removed and int is inserted manually as the return type. As shown, the compiler has no way of knowing what to make of R. How can I deduce function return types in C++03? I'm looking for methods that do not require

Does a reference declaration introduce a new name for the referent?

淺唱寂寞╮ 提交于 2020-01-01 01:59:08
问题 In this question we've learnt that RVO cannot be applied to an expression like p.first . In comments it was also suggested that RVO is generally not applied to an expression like r after a declaration like auto& r = p.first . It is less clear whether the standard mandates this behaviour. in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function parameter or a variable introduced by the exception

Is member value in the class initialized when an object is created?

守給你的承諾、 提交于 2019-12-31 05:15:36
问题 I'm writing a hash class: struct hashmap { void insert(const char* key, const char* value); char* search(const char* key); private: unsigned int hash(const char* s); hashnode* table_[SIZE]; // <-- }; As insert() need to check if table[i] is empty when inserting a new pair, so I need all pointers in the table set to NULL at start up. My question is, will this pointer array table_ be automatically initialized to zero, or I should manually use a loop to set the array to zero in the constructor?

Can friend class be declared conditionally in C++03?

 ̄綄美尐妖づ 提交于 2019-12-31 01:47:07
问题 I want to declare a friend class only if some (compile-time) condition is true. For example: // pseudo-C++ class Foo { if(some_compile_time_condition) { friend class Bar; } }; I did not find any solution on the internet. I went through all the answers to the question Generating Structures dynamically at compile time. Many of them use the C++11 std::conditional , but I would like to know if it is possible to do this in C++03 without using the preprocessor . This solution https://stackoverflow