c++14

How to provide multiple begin/end proxies for a class

一笑奈何 提交于 2019-12-08 04:58:58
问题 Given the classes struct Data { void bar() const; void baz(); } class Foo { std::vector<Data> data; std::map<size_t, Data> indexed_data; } I'd like to implement something in class Foo so that I can do the following: int main() { Foo foo; for(const auto& data : foo.data()) data.bar(); for(auto& data : foo.indexed_data()) data.baz(); const auto& foo_ref = foo; for(auto& data : foo_ref.data()) data.baz(); // constness violated, shouldn't compile } However, I don't wanna expose the class

Compile time hash with constexpr

落花浮王杯 提交于 2019-12-08 04:14:03
问题 I have found this example/class in a book for creating SDBM hashes at compile time. Unfortunately it does not compile (neither with c++11 nor c++14). I am getting error: call to non-constexpr function . I've tried around a little bit, but I can't seem to make this work. So here is my question: Why is it not working and how could it be fixed? (I am sorry, I know it's a generic question, but at least for a very specific case) Full (not working) example for you to test: #include <iostream>

Compile time hash with constexpr

試著忘記壹切 提交于 2019-12-08 04:03:24
I have found this example/class in a book for creating SDBM hashes at compile time. Unfortunately it does not compile (neither with c++11 nor c++14). I am getting error: call to non-constexpr function . I've tried around a little bit, but I can't seem to make this work. So here is my question: Why is it not working and how could it be fixed? (I am sorry, I know it's a generic question, but at least for a very specific case) Full (not working) example for you to test: #include <iostream> template <int stringLength> struct SDBMCalculator { static inline int Calculate(const char* const

trivially moveable but not trivially copyable

两盒软妹~` 提交于 2019-12-08 02:15:24
问题 Is it possible to make class type, which is: trivially move-constructible, but not trivially copy-constructible, but still copy-constructible trivially copy-constructible, but not trivially move-constructible, but still move-constructible trivially copy-assignable, but not trivially move-assignable, but still move-assignable trivially move-assignable, but not trivially copy-assignable, but still copy-assignable trivially copy/move-constructible, but not trivially copy/move-assignable, but

Returning a class from a constexpr function requires virtual keyword with g++

浪尽此生 提交于 2019-12-08 02:14:58
问题 Hi the following program works with g++ 4.9.2 (Ubuntu 4.9.2-10ubuntu13) but the virtual keyword is required for the function get : //g++ -std=c++14 test.cpp //test.cpp #include <iostream> using namespace std; template<typename T> constexpr auto create() { class test { public: int i; virtual int get(){ return 123; } } r; return r; } auto v = create<int>(); int main(void){ cout<<v.get()<<endl; } If I omit the virtual keyword, I get the following error : test.cpp: In instantiation of ‘constexpr

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

余生长醉 提交于 2019-12-08 02:01:50
问题 #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. 回答1: 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) {

Local Static variable initialization is thread safe [duplicate]

天大地大妈咪最大 提交于 2019-12-08 00:26:30
问题 This question already has answers here : Is local static variable initialization thread-safe in C++11? [duplicate] (2 answers) Closed 3 years ago . Suppose I have a class with three static functions like this : #include <vector> #include <iostream> using namespace std; #include <thread> class Employee { }; class client { public: void Doprocessing() { //is this thread safe in c++11/14 static int i = CreateEmployee(); //does it look good to use static variable like this to make it thread safe?

Range/Loop through N variables in [modern] C++

一笑奈何 提交于 2019-12-07 22:37:52
问题 What's a succinct way of ranging through N variables, of any type each, to perform an operation? Let's say I have variables a , b , c , d , e and want to go through all of them performing some operation. 回答1: Use Boost.Hana and generic lambdas: #include <tuple> #include <iostream> #include <boost/hana.hpp> #include <boost/hana/ext/std/tuple.hpp> struct A {}; struct B {}; struct C {}; struct D {}; struct E {}; int main() { using namespace std; using boost::hana::for_each; A a; B b; C c; D d; E

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

我的梦境 提交于 2019-12-07 18:24:35
问题 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

gcc vs clang - ambiguous overload when using `make_overload` variadic lambda inheritance [duplicate]

隐身守侯 提交于 2019-12-07 16:39:44
问题 This question already has answers here : Overloaded lambdas in C++ and differences between clang and gcc (2 answers) Closed 4 years ago . Time for another round of clang vs gcc . Live example on godbolt.org. Test 0 : overloaded callable object struct Trad { auto operator()(int) { return 1; } auto operator()(float) { return 2; } auto operator()(double) { return 3; } }; int main() { assert(Trad{}(1) == 1); assert(Trad{}(1.f) == 2); assert(Trad{}(1.0) == 3); } g++ 5.2 compiles and run. clang++ 3