c++14

Exception safety issue of std::make_unique()

一曲冷凌霜 提交于 2019-12-06 11:53:08
问题 From this thread, we know that the following code is exception-safe (assuming that T( U(std::move(v)) ) is exception-safe, e.g., does not throw, which is library user's responsibility). auto p = new T( U(std::move(v)) ); The key point here is that the initializer expression U(std::move(v)) is evaluated after memory allocation. Now consider the std::make_unique() counterpart. auto p = std::make_unique<T>( U(std::move(v)) ); The initializer expression U(std::move(v)) is evaluated even before

Check if member function exists and is not inherited for SFINAE

情到浓时终转凉″ 提交于 2019-12-06 09:21:04
问题 How can I check if a member function exists and is not inherited? I need this to resolve ambiguity for the following example: A type either has a foo() or a bar() member function. Caller will call the one that exists for the given type. However, DerivedWithBar inherits foo() from BaseWithFoo but defines its own bar() . Thus, Caller does not know which function to call. I'd need a way to give the non-inherited foo precedence over the inherited bar() , but I do not know how to check if a member

constexpr in C++11 and C++14 (not a difference to const keyword) [duplicate]

吃可爱长大的小学妹 提交于 2019-12-06 09:18:42
This question already has answers here : Difference between `constexpr` and `const` (8 answers) Closed 2 years ago . In the "Exploring C++17 and beyond" presentation by Mike Isaacson at one point ( https://youtu.be/-ctgSbEfRxU?t=2907 ) there is question about writing: const constexpr .... vs single const. Mike said that in C++11 constexpr implies const and in C++14 it does not. Is it true? I've tried to find prove for that but I couldn't. I'm not asking about difference between const and constexpr (as a lot of other questions) but about difference in constexpr in two versions of C++ standard.

How to use vectors (of C++ STL) with WebAssembly

╄→гoц情女王★ 提交于 2019-12-06 08:53:13
#include <iostream> #include<vector> using namespace std; vector<int> ver; int pushData(int n) { for(int i=0;i<n;i++) { ver.push_back(i); } } I want to call pushData function from JS and push some data to vector "ver" and use it later. please explain how to do that using WebAssembly. I tried to answer your question, by using this answer: https://stackoverflow.com/a/46748966/544721 to make solution : #include<vector> const int SIZE = 10; std::vector<int> data(10); void add(int value) { for (int i=0; i<SIZE; i++) { data[i] = data[i] + value; } } int* getData() { return &(data[0]); } and js: var

How to track memory assign by STL library

五迷三道 提交于 2019-12-06 08:44:56
问题 I want to track all the memory(size allocated by std lib) allocated by all STL containers like map,list,vector etc. I just want to track STL container not regular object creation. Basically want to override new and delete of std lib. Example class demo { public: int i; std::list<int> mylist; } int main() { demo dd = new demo(); // -> Don't want to track this. Just want to track // mylist(size of my list) } I found out that std has it's own allocator option. For example list has it is

invalid initialization of non-const reference from an rvalue

三世轮回 提交于 2019-12-06 08:41:30
问题 So I have the following function: void scan(std::istream& is, Handler& h); I want to call it in different ways, like: scan(std::cin, Handler()); scan(std::ifstream("myfile"), myhandler); The compiler complains about std::ifstream("myfile") and Handler() of being rvalues being passed as non-const references, so the complaint is legitimate, but what can I do? Neither function parameters cannot be const ( istream is modified while read and the handler changes its state during callbacks). If I

Overloaded output operator not found for Boost.Spirit expression

巧了我就是萌 提交于 2019-12-06 07:31:14
This is a follow-up on this Q&A . I now have several data structures in a namespace ast , subdivided over two sub-namespaces ( algebraic and numeric ) that correspond to the two different formats that the grammar recognizes. namespace ast { namespace algebraic { struct occupance { char pc; char col; int row; }; using pieces = std::vector<occupance>; struct placement { char c; boost::optional<pieces> p; }; } namespace numeric { struct occupance { char pc; int sq; }; struct range { occupance oc; int sq; }; using pieces = std::vector<boost::variant<range, occupance>>; struct placement { char c;

Undefined reference to friend function template defined inside class in a namespace

主宰稳场 提交于 2019-12-06 07:30:32
This is a follow-up on my answer to this question . The original answer I posted does not have any namespaces, and it solves the problem. However, the OP subsequently made an edit claiming that the solution does not work if the class is inside a namespace. My initial reaction was that you can make it work by simply having using N::f; either at global scope or inside main() (or any other function). While that is certainly the case, the OP (justifiably) commented that this is not ideal, and I agree. Nevertheless, I still thought that calling N::f without having using N::f; should work just fine,

Change array dimensions at runtime

与世无争的帅哥 提交于 2019-12-06 07:13:47
I have this matrix class which has a 2d double array. In the constructor you can specify the width and height. I want to create a 1d array instead of a 2d when the width is 1. Because I overloaded the [] operator and return the pointer. If there is only 1 row/col I don't want to always write [i][0]. Instead I want to just write [i]. Does anyone know how to solve this? Edit: To clarify this I need this class for matrix calculations no just as array. Justin Time 2 Reinstate Monica I would recommend using a one-dimensional array, as user2079303 suggested in their answer . If this isn't desirable,

What are the syntax and semantics of C++ templated code?

▼魔方 西西 提交于 2019-12-06 06:51:34
template<typename T, size_t M, size_t K, size_t N, typename std::enable_if_t<std::is_floating_point<T>::value, T> = 0> void fastor2d(){//...} I copied this line of code from cpp-reference(only the std::enable_if part, i do need T and all three of the size_t 's), because i would like to use this function only when floating_types are used on it ... it does not compile. Could somebody explain to me, why, and what it even does? While i am at it, how do you call this function afterwards? Every tutorial or question here on SO gets bombed with answers, and that is great, but to someone who does not