c++14

Overload a method in a way that generates a compiler error when called with a temporary

假如想象 提交于 2019-12-10 16:09:56
问题 Perhaps this piece of code will illustrate my intent best: #include <array> template <size_t N> void f(std::array<char, N> arr) { } template <size_t N> void f(std::array<char, N>&& arr) { static_assert(false, "This function may not be called with a temporary."); } f() should compile for lvalues but not for rvalues. This code works with MSVC, but GCC trips on the static_assert even though this overload is never called. So my question is two-fold: how to express my intent properly with modern C

How operator<< with boost::variant is implemented

时光毁灭记忆、已成空白 提交于 2019-12-10 16:09:44
问题 I understand that boost::variant is implemented something like so template <typename... Vs> struct variant { std::aligned_union<Vs...>::type buffer; .... }; How can we make an operator<< for a struct like this that prints the casts the type stored in the buffer and passes that to operator<< for cout ? For this we would need to know the type of the element stored in the buffer right? Is there a way to know this? Also I am looking for an explanation of such an implementation, if one exists. Not

reinterpret_cast to the same type

て烟熏妆下的殇ゞ 提交于 2019-12-10 16:01:44
问题 Consider following program: struct A{}; int main() { A a; A b = a; A c = reinterpret_cast<A>(a); } The compiler(g++14) throws an error about invalid cast from type 'A' to type 'A' . Why is casting to the same type invalid? 回答1: It is not allowed, because the standard says so. There is a rather limited set of allowed conversion that you can do with reinterpret_cast . See eg cppreference. For example the first point listed there is: 1) An expression of integral, enumeration, pointer, or pointer

Algorithm to deal with set intervals

落花浮王杯 提交于 2019-12-10 15:59:14
问题 What is the best way to go about implementing a class which needs to keep track of set intervals in C++? I was hoping to leverage existing STL or Boost libraries but other than using standard containers, I had to resort to implementing the algorithm by hand which was surprisingly tricky to get right and I had to simplify it at the cost of some performance because of a work deadline. There must be a better way. For example, I need the following type of behaviour: class SubRange { public:

Safely Destroying a Thread Pool

我们两清 提交于 2019-12-10 15:37:29
问题 Consider the following implementation of a trivial thread pool written in C++14. threadpool.h threadpool.cpp Observe that each thread is sleeping until it's been notified to awaken -- or some spurious wake up call -- and the following predicate evaluates to true : std::unique_lock<mutex> lock(this->instance_mutex_); this->cond_handle_task_.wait(lock, [this] { return (this->destroy_ || !this->tasks_.empty()); }); Furthermore, observe that a ThreadPool object uses the data member destroy_ to

constexpr with untouched non-constexpr arguments: Who is correct, clang or gcc?

送分小仙女□ 提交于 2019-12-10 15:24:05
问题 I have 4 test cases and I believe that all of them are valid: constexpr int f(int const& /*unused*/){ return 1; } void g(int const& p){ constexpr int a = f(p); // clang error, gcc valid int v = 0; constexpr int b = f(v); // clang valid, gcc valid int const& r = v; constexpr int c = f(r); // clang error, gcc error int n = p; constexpr int d = f(n); // clang valid, gcc valid } int main(){ int p = 0; g(p); } Clang and GCC differ only in the first test case. I tested with clang 4 & 5 (20170319)

Specializing std::hash for derived classes works in gcc, not clang

六月ゝ 毕业季﹏ 提交于 2019-12-10 15:18:11
问题 I am trying to specialize std::hash for derved classes. The best approach so far is based on this answer: #include <type_traits> #include <functional> #include <unordered_set> namespace foo { template<class T, class E> using first = T; struct hashable {}; struct bar : public hashable {}; } namespace std { template <typename T> struct hash<foo::first<T, std::enable_if_t<std::is_base_of<foo::hashable, T>::value>>> { size_t operator()(const T& x) const { return 13; } }; } int main() { std:

Lambda expression to return bool in if statement

爷,独闯天下 提交于 2019-12-10 15:15:20
问题 Just to get to the point, I want to return true or false using the lambda expression inside the if() statement. I saw this question which has similar question to mine: LINK but I could not find the answer. So here is my example code: if([&rel_pose](Eigen::VectorXd pose) { return (sqrt(pose(0) * pose(0) + pose(1) * pose(1)) < 2) ? true : false; }) // do smth When I try to compile I get this error: error: could not convert ‘<lambda closure object>graphslam::GraphSLAM::main_pose_callback(const

Concatenating mismatched string WORKS in VC2015 - How?

馋奶兔 提交于 2019-12-10 15:09:52
问题 When we have either of these: auto city1 = "New " L"Delhi"; auto city2 = L"New " "York"; Any pre-VS2015 compiler would raise error: error C2308: concatenating mismatched strings But with VC2015 compiler, it compiles well and the resultant type ( auto deduction) is a wide-char string. My question is: When and How this is made possible - any standard specification? 回答1: In C++03 this behaviour would be undefined. ISO 14882-2003: 2.13.4.3 states that In translation phase 6 (2.1), adjacent narrow

Create a variadic template from a constexpr array

这一生的挚爱 提交于 2019-12-10 14:56:43
问题 Let's say we have the following type template <bool... Values> struct foo{}; I want to create a variadic template from a constexpr array bool tab[N] . In other words, I want to do something like: constexpr bool tab[3] = {true,false,true}; using ty1 = foo<tab[0], tab[1], tab[2]>; But I want to do it programmatically. For now, I've tried the following: template <std::size_t N, std::size_t... I> auto mk_foo_ty(const bool (&tab)[N], std::index_sequence<I...>) { // error: template argument for