c++03

How to create a std::map of constant values which is still accessible by the [] operator?

你说的曾经没有我的故事 提交于 2019-12-01 17:27:09
问题 I need a std:map data structure that is read only, which means I have to fill it once with data and then only read those values, never change them or add additional ones. My non-const version looks like this: //in .h #include <string> #include <map> std::map<std::string, int> myMap; void initMap(); //in .cpp #include "foo.h" void initMap() { myMap["Keys"] = 42; } Then I'd call initMap() once in my code and be done. Now I've read several questions here already and it seems non-trivial to

C++98/03 reference collapsing and cv qualifiers

此生再无相见时 提交于 2019-12-01 17:00:40
问题 The code below compiles (gcc 4.7.2 or icc 13) and produces "1 2" output. Which means that const qualifier is dropped, i. e., f<int&> has the parameter type int& . Why does it happen? As I understand, according to §14.3.1.4: If a template-argument for a template-parameter T names a type “reference to cv1 S ”, an attempt to create the type “reference to cv2 T ” creates the type “reference to cv12 S ”, where cv12 is the union of the cv-qualifiers cv1 and cv2 . Redundant cv-qualifiers are ignored.

How to swap two __m128i variables in C++03 given its an opaque type and an array?

99封情书 提交于 2019-12-01 08:35:34
What is the best practice for swapping __m128i variables? The background is a compile error under Sun Studio 12.2 , which is a C++03 compiler. __m128i is an opaque type used with MMX and SSE instructions, and its usually and unsigned long long[2] . C++03 does not provide the support for swapping arrays, and std:swap(__m128i a, __m128i b) fails under the compiler. Here are some related questions that don't quite hit the mark. They don't apply because std::vector is not available. How can we swap 2 arrays in constant complexity or O(1)? Is it possible to swap arrays of structs C++03 moving a

How to prevent derivation from a type in c++03? [duplicate]

北城以北 提交于 2019-12-01 05:44:47
问题 This question already has answers here : Simpler “Preventing derived classes” in C++ (2 answers) Closed 5 years ago . C++11 introduces keyword final , which makes it illegal to derive from a type. Is there a way to achieve a similar result with C++03, perhaps by making certain member functions private ? 回答1: There are two solutions for C++03 : First solution: private virtual friend base class with private default constructor: Based on this answer, with the difference that template are not

Are there any cases where it is incorrect to replace push_back with emplace_back?

吃可爱长大的小学妹 提交于 2019-12-01 03:31:39
Can I break a valid C++03 program by replacing std::vector::push_back with emplace_back and compiling it with C++ 11 compiler? From reading emplace_back reference I gather it shouldn't happen, but I'll admit I don't fully get rvalue references. I constructed a short example that actually fails to compile when push_back is replaced by emplace_back : #include <vector> struct S { S(double) {} private: explicit S(int) {} }; int main() { std::vector<S>().push_back(0); // OK std::vector<S>().emplace_back(0); // error! } The call to push_back needs to convert its argument 0 from type int to type S .

Vector of structs with const members?

孤人 提交于 2019-12-01 03:11:10
Let's say I have #include <string> #include <vector> using namespace std; struct Student { const string name; int grade; Student(const string &name) : name(name) { } }; How do I, then, keep a vector of students? int main() { vector<Student> v; // error C2582: 'operator =' function is unavailable in 'Student' v.push_back(Student("john")); } Is there even a way to do this, or must I allocate all the students on the heap, and store a pointer to each of them instead? You can't. Your type violates the "Assignable" requirement for standard containers. ISO/IEC 14882:2003 23.1 [lib.container

Why doesn't PRIu64 work in this code?

馋奶兔 提交于 2019-12-01 02:10:53
As per this answer , I tried printing a uint64_t , but it gives me an error: error: expected ``)' before 'PRIu64' Following is the minimal code showing what I am trying to do: #define __STDC_FORMAT_MACROS #include <inttypes.h> #include <cstdio> class X { X() { uint64_t foo = 0; printf("%07" PRIu64 ": ", foo); } }; int main() {} This minimal code compiles, but my actual code does not. However, I have tried with the 2 lines inside X::X() exactly the same in my actual code, and that does not work. What should I look for to debug this further? My actual code also #include s other headers. Could

Two-dimensional vector printing

依然范特西╮ 提交于 2019-12-01 00:56:49
I've got a two-dimension string vector that I need to print out. The whole program should read a line from a txt file, store each word from it as a different element and then push the "word vector" into a vector that contains for example 100 lines. I've got everything going, but the problem comes out when I have to print the vector. Every line can have a different number of words, ex: I like cake a lot. So I can't use: for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << vec[i][j]; } } because the second line doesn't contain 3 elements and the program closes. Any idea how to do

Does “potentially-evaluated” means the same as “odr-used” in C++03?

左心房为你撑大大i 提交于 2019-11-30 20:39:47
Given an example: #include <iostream> class A { public: static const int numberOfWheels = 4; }; // const int A::numberOfWheels; int main() { std::cout << A::numberOfWheels << std::endl; } Is it formally undefined behavior( UB ) since A::numberOfWheels is used without its definition? (see also here ). As C++03 states: The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer. I found that definition of used in C++03 is rather confusing, as it points to potentially-evaluated expression: An object or non

Two-dimensional vector printing

故事扮演 提交于 2019-11-30 19:02:46
问题 I've got a two-dimension string vector that I need to print out. The whole program should read a line from a txt file, store each word from it as a different element and then push the "word vector" into a vector that contains for example 100 lines. I've got everything going, but the problem comes out when I have to print the vector. Every line can have a different number of words, ex: I like cake a lot. So I can't use: for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << vec[i]