Davidbrcz

41

votes
6

answer
4.8k

views

Why are std::begin and std::end “not memory safe”?

In this blog post, Eric Niebler states that: What is wrong with std::begin and std::end? Surprise! they are not memory safe. Consider what this code does: extern std::vector get_data(); auto it = std::begin(get_data()); int i = *it; // BOOM std::begin has two overloads for const and non-const lvalu...
Davidbrcz
4

votes
2

answer
30

views

safety of std::begin and std::end

In this blog post, Eric Niebler states that: What is wrong with std::begin and std::end? Surprise! they are not memory safe. Consider what this code does: extern std::vector get_data(); auto it = std::begin(get_data()); int i = *it; // BOOM std::begin has two overloads for const and non-const lvalu...
Davidbrcz
1

votes
1

answer
75

views

How to get a warning on old exception specifications

I know that among other things (like auto_ptr), exception specifications have been deprecated in C++11. Is there any way to get a warning from g++ 4.8 with the following code ? struct F{ void foo() throw (int){} }; int main() { } I already tried with -Wall -pedantic -Wextra -Wdeprecated-declarations...
Davidbrcz
1

votes
2

answer
38

views

Delete all lines that match a given beginning and 2 possible ends

I want to suppress into a file all the lines that match the following requirements : The line begin with ' *' (a space followed by a star) The line ends with either '.cpp' or '.hpp' I dont care about the content between that So for instance, this should show up * ns1/file.cpp * file2.hpp...
Davidbrcz
4

votes
2

answer
316

views

In class initialization and initializer list

I have recently discovered that you cant have at the same time in class initialization and initializer list. The following code fails : struct s { int i=0; }; int main() { s s1; //s1.i = 0 //s s2={42}; //fails return 0; } If I remove the in class initialization, the initializer list works fine !...
Davidbrcz
2

votes
2

answer
284

views

Return a copy from a function of a non moveable object

A friend of mine stumbled on a question Is there any way to return a copy of an object which is copy-able but NOT move-able. In other words, can we make the following code work? struct A { A() = default; A(A const&) = default; // copyable A& operator=(A const&) = default; // assignable A(A &&) = d...
Davidbrcz
8

votes
1

answer
641

views

Can std::atomic be safely used with OpenMP

I'm currently trying to learn ow to use OpenMP and I have a question. Is it safe to do something like that : std::atomic result; #pragma omp parallel for for(...) { result+= //some stuff; } Or shall I use : double result; #pragma omp parallel for for(...) { double tmp=0; //some stuff; #pragma omp a...
Davidbrcz
3

votes
0

answer
404

views

CXXMethodDecl::getNameAsString for template class constructor or destructor

I'm writing a small software in C++ with libtooling to translate C++ headers into C++ skeleton files. It works fine for non-template class but I'm having an issue with template classes, especially with template class's constructor or destructor. I'm using CXXMethodDecl::getNameAsString to get the n...
Davidbrcz
3

votes
3

answer
1.9k

views

Choose between template function and auto type deduction

I have a generic question about template functions versus auto type deduction for functions. For years, we have have been able to write template function : template T add(T a,T b){ return a+b; } There is a TS for using auto for function's parameters deduction auto add(auto a,auto b){ return a+b; }...
Davidbrcz