c++03

Compile-time error for non-instantiated template members instead of link-time error

被刻印的时光 ゝ 提交于 2019-12-06 03:50:47
I have template class ItemContainer that actually is facade for a whole family of containers with different capabilities like sorting, indexing, grouping etc. Implementation details are hidden in cpp. file using pimpl idiom and explicit instantiation. Template is instantiated only with well-known limited set of implementation classes that define the actual behavior of container. Main template implements common functions supported by all containers - IsEmpty() , GetCount() , Clear() etc. Each specific container specializes some functions that are supported only by it, e.g. Sort() for sorted

C++03 moving a vector into a class member through constructor (move semantics)

大兔子大兔子 提交于 2019-12-06 01:27:15
问题 I only have access to C++03 and I often want to move a vector into a function the way you can do it in C++11. The question how to do it not to confuse the user of the code too much. So my question is how did programmers do it before C++11. I know that vector can be "moved" using swap function. So here is what I have come up with: class Foo { public: Foo(std::vector<int>& vec) { using std::swap; swap(vec, m_vec); // "move" vec into member vector } private: std::vector<int> m_vec; }; // usage:

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

那年仲夏 提交于 2019-12-06 01:22:45
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)? 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_t tmp; unsigned char * p = reinterpret_cast<unsigned char *>(&tmp); unsigned char * q = reinterpret_cast

Why use = to initialise a primitive type in C++?

落花浮王杯 提交于 2019-12-05 22:05:36
问题 Where I work, people mostly think that objects are best initialised using C++-style construction (with parentheses), whereas primitive types should be initialised with the = operator: std::string strFoo( "Foo" ); int nBar = 5; Nobody seems to be able to explain why they prefer things this way, though. I can see that std::string = "Foo"; would be inefficient because it would involve an extra copy, but what's wrong with just banishing the = operator altogether and using parentheses everywhere?

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

喜夏-厌秋 提交于 2019-12-05 20:42:05
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 following are not available: lambdas Lightness Races with Monica My understanding is that std::bind and boost:

Template specialization for multiple types

帅比萌擦擦* 提交于 2019-12-05 20:28:28
问题 Title is a little ambiguous. Lets say I have a template defined as: template < typename T > void foo ( int x ) ; template <> void foo<char> ( int x ) ; template <> void foo<unsigned char> ( int x ) ; template <> void foo<short> ( int x ) ; ... Internally both foo<signed>() and foo<unsigned>() do exactly the same thing. The only requirement is that T be an 8bit type. I could do this by creating another template to type define a standard type based on size. template < typename T, size_t N =

POD structs containing constant member

坚强是说给别人听的谎言 提交于 2019-12-05 19:09:25
问题 With this code: struct A { int i; const int b; }; // The union is to verify that A is a type that can be used in a union. union U { A a; int b; }; int main() { U a = {1, 1}; U b = {2, 1}; } g++ version 4.8.3 complains about an error: a.cpp:9:4: error: member ‘A U::a’ with copy assignment operator not allowed in union A a; ^ a.cpp:9:4: note: unrestricted unions only available with -std=c++11 or -std=gnu++11 but clang 3.5.0 compiles this code without error. Which one is correct? Is this a

How to get the value type from an output iterator?

安稳与你 提交于 2019-12-05 14:30:01
问题 Let's say that I have a C container (e.g., MyContainer ) with contained objects stored as void* pointers. The only way to iterate through the elements of this container is via two interface functions: getFirstElem(MyContainer const&, void*) : Outputs the first element of the container. getNextElem(MyContainer const&, void*) : Outputs the next element of the container. I want to code a generic function that iterates through the elements of this C container via the interface functions mentioned

Getting the type of a member

廉价感情. 提交于 2019-12-05 14:11:18
问题 NOTE : This question was originally asked way back in 2012. Before the decltype specifier was fully implemented by any major compilers. You should not be looking at this code unless you only have access to C++03. All major C++11 compliant compilers now support decltype . Is there an easy way to retrieve the type of a member? In C++03 struct Person { std::string name; int age; double salary; }; int main() { std::vector<Person> people; // get a vector of people. std::vector<GET_TYPE_OF(Person:

Using boost to create a lambda function which always returns true

[亡魂溺海] 提交于 2019-12-05 09:02:10
Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)> predicate ); If I want to call it with a predicate that always returns true, I can define a helper function: bool AlwaysTrue( int, int, int ) { return true; } ... Foo( boost::bind( AlwaysTrue ) ); But is there anyway to call this function (possibly using boost::lambda) without having to define a separate function? [Edit: forgot to say: I CAN'T use C++0x] UncleBens commented on this in Scharron's answer, but I think it is actually the best answer so I'm stealing it (sorry UncleBens).