c++14

Correct way to pause & resume an std::thread

て烟熏妆下的殇ゞ 提交于 2019-12-19 06:43:43
问题 I am using an std::thread in my C++ code to constantly poll for some data & add it to a buffer. I use a C++ lambda to start the thread like this: StartMyThread() { thread_running = true; the_thread = std::thread { [this] { while(thread_running) { GetData(); } }}; } thread_running is an atomic<bool> declared in class header. Here is my GetData function: GetData() { //Some heavy logic which needs to be executed in a worker thread } Next I also have a StopMyThread function where I set thread

T* versus char* pointer arithmetic

喜你入骨 提交于 2019-12-19 05:23:07
问题 Assume we have an array that contains N elements of type T. T a[N]; According to the C++14 Standard , under which conditions do we have a guarantee that (char*)(void*)&a[0] + n*sizeof(T) == (char*)(void*)&a[n], (0<=n<N) ? While this is true for many types and implementations, the standard mentions it in a footnote, and in an ambiguous way: §5.7.6, footnote 85) Another way to approach pointer arithmetic ... There is little indication that this other way was thought of being equivalent to the

Different overloads with std::function parameters is ambiguous with bind (sometimes)

别说谁变了你拦得住时间么 提交于 2019-12-19 04:01:08
问题 I have two overloads of a function foo which take different std::function s which results in an ambiguity issue for the latter when used with the result of a std::bind . I don't understand why only this is ambiguous. void foo(std::function<void(int)>) {} void foo(std::function<int()>) {} void take_int(int) { } int ret_int() { return 0; } When using int() with a bind function I get an ambiguity error foo(std::bind(ret_int)); // ERROR With the gcc-5.1 error (and similar with clang) error: call

Compiling program as c++ 14 in Sublime Text 3 as default

霸气de小男生 提交于 2019-12-19 03:08:34
问题 I know that we can compile program as C++ using g++ compiler. But g++ compiler defaults to 98 version. To run it as C++ 14, we need to add -std=c++14 in terminal. Sublime Text is considered a valuable editor for competitive programming due to its light weight and features. In these competitions time is important and thus the time is wasted in copying in text file and then running from terminal. C++ 14 features a rich library and other important features in comparison to 98. Thus one would

Which compiler, if any has a bug in parameter pack expansion?

泪湿孤枕 提交于 2019-12-18 19:40:23
问题 When experimenting with convenient ways to access tuples as containers, I wrote a test program. on clang (3.9.1, and apple clang) it compiles as expected, producing the expected output: 1.1 foo 2 on gcc (5.4, 6.3), it fails to compile: <source>: In lambda function: <source>:14:61: error: parameter packs not expanded with '...': +[](F& f, Tuple& tuple) { f(std::get<Is>(tuple)); }... ^ <source>:14:61: note: 'Is' <source>: In function 'decltype(auto) notstd::make_callers_impl(std::index_sequence

Is it possible to check for existence of member templates just by an identifier?

大兔子大兔子 提交于 2019-12-18 19:32:13
问题 Can we detect member function template , variable template , class / struct / union template or alias template without knowing amount, or nature of template / non-template parameters? When I try to think about this, nothing really comes to my mind. But let's have structure with member function template: struct foo { // Really random. Let's assume we don't know this declaration, just the name "bar" template <class T, std::size_t N, class... Args> void bar(T a, T b, T(&c)[N], Args const& ...);

Why is this not a constant expression?

▼魔方 西西 提交于 2019-12-18 18:46:49
问题 In this trivial example, test2 fails to compile even though test1 succeeds, and I don't see why that is the case. If arr[i] is suitable for a return value from a function marked constexpr then why can it not be used as a non-type template argument? template<char c> struct t { static const char value = c; }; template <unsigned N> constexpr char test1(const char (&arr)[N], unsigned i) { return arr[i]; } template <unsigned N> constexpr char test2(const char (&arr)[N], unsigned i) { return t<arr

Iterating on a tuple… again

╄→гoц情女王★ 提交于 2019-12-18 16:32:11
问题 It's been a while that I've been doing C++ but I'm not familiar with templates. Recently, I tried to write a class that wrap a std::vector<std::tuple<Types...>> . This class must have member functions, and I really need to be able to iterate over the tuple. In fact, if I am able to print every element of a tuple (in the order), I would be able to do everything I need. I found a solution using a cast, but I'm not really confident with it since it is based on a cast that I don't really like

Maybe my understanding of [class.access]/7 isn't correct, but

社会主义新天地 提交于 2019-12-18 15:17:10
问题 From [class.access]/7 we have the following sentence: Similarly, the use of A::B as a base-specifier is well-formed because D is derived from A , so checking of base-specifier s must be deferred until the entire base-specifier-list has been seen. class A { protected: struct B { }; }; struct D: A::B, A { }; See live example with clang. As a matter of fact, clang also complains about this snippet, where no deferment is necessary. class A { protected: struct B { }; }; struct D: A, A::B { }; Why

Why aren't C++14 standard-defined literals in the global namespace by default?

限于喜欢 提交于 2019-12-18 14:38:31
问题 C++14 includes standard-defined literals for, amongst other things, std::string and various timespans from the <chrono> header. To use them you must say using namespace std::literals; (or some variation depending on exactly which literals you want, as they're in a variety of inline namespaces). All this is good, but I'm curious as to why the using declaration is required. UDLs without a leading underscore are reserved for the implementation, so there is no possibility that "hello world"s