c++17

Why do conversion operators cause ambiguous overload when const-ref and value exist

无人久伴 提交于 2021-01-28 02:45:14
问题 I'm looking at a wrapping class, based on https://www.fluentcpp.com/category/strong-types/ The main difference is that I'm replacing the get() method with a explicit casting operator as this triggers questions during code review when used. As you can see in the simplified code below, I have 3 overloads of the casting operator: From const A & to int From A && to int From const A & to const int & When writing: static_cast<int>(a) , I expect the overload of const A & to int to be used. However,

What is the correct way to link C++17 filesystem with CMake?

帅比萌擦擦* 提交于 2021-01-28 01:49:10
问题 I noticed that linking my CMake project with gcc 8.3 fails to link functions from std::filesystem. This is not the case with gcc 9, clang 7 or clang 8. I found solutions like this and this, but these hard-code the linking of stdc++fs , which is normally not what you want to do. So what is the correct way to link such libraries? Do I have to do a find_package ? What would be the package I am looking for? 回答1: It looks like there is no proper solution to this as of now. There still is an open

How to implement universal switch/case, which can work for general C++ types as well and syntactically similar?

回眸只為那壹抹淺笑 提交于 2021-01-27 22:57:25
问题 In C/C++, switch/case compares only an integral type with a compile time constants. It's not possible to use them to compare user/library defined types like std::string with runtime values. Why the switch statement cannot be applied on strings? Can we implement look-a-like switch/case which gives similar syntactic sugar and serves the purpose of avoiding plain if/else comparisons. struct X { std::string s; bool operator== (const X& other) const { return s == other.s; } bool operator== (const

std::shared_ptr which is empty but not null

无人久伴 提交于 2021-01-27 22:08:38
问题 http://www.cplusplus.com/reference/memory/shared_ptr/ says A shared_ptr that does not own any pointer is called an empty shared_ptr. A shared_ptr that points to no object is called a null shared_ptr and shall not be dereferenced. Notice though that an empty shared_ptr is not necessarily a null shared_ptr, and a null shared_ptr is not necessarily an empty shared_ptr. Am I able to create an empty std::shared_ptr, i.e. one which does not own anything but which is not null, i.e. contains payload?

Why do conversion operators cause ambiguous overload when const-ref and value exist

ぃ、小莉子 提交于 2021-01-27 22:04:15
问题 I'm looking at a wrapping class, based on https://www.fluentcpp.com/category/strong-types/ The main difference is that I'm replacing the get() method with a explicit casting operator as this triggers questions during code review when used. As you can see in the simplified code below, I have 3 overloads of the casting operator: From const A & to int From A && to int From const A & to const int & When writing: static_cast<int>(a) , I expect the overload of const A & to int to be used. However,

Would a template work for std::variant's visit?

和自甴很熟 提交于 2021-01-27 20:06:40
问题 Earlier I asked this question about std::variant . Considering that the types hold by the variant are all printable by std::cout , is there a simple way to implement a visitor? Here for example, all the way down you have several lambdas to cover each type, but all do the same thing (except std::string ): std::cout << arg << ' '; . Is there a way to not repeat my self? std::visit(overloaded { [](int arg) { std::cout << arg; }, [](long arg) { std::cout << arg; }, [](double arg) { std::cout <<

Template argument deduction fails when using braced initializer list

妖精的绣舞 提交于 2021-01-27 19:03:17
问题 I'm trying to use template argument deduction in the 'perpendicular()' function: #include <iostream> template <typename component = double> struct offset { component x; component y; }; template <typename component> offset(component x, component y) -> offset<component>; template <typename component> offset<component> perpendicular(offset<component> const &o) { return offset{o.y, -o.x}; } template <typename component> std::ostream &operator<<(std::ostream &s, offset<component> const &o) {

constexpr-ness of std::initializer_list::size() vs std::array::size()

孤者浪人 提交于 2021-01-27 18:38:10
问题 The size() member functions of std::initializer_list and std::array have identical signatures: constexpr size_type size() const noexcept; Both are constexpr . However, std::array::size() can be used in constexpr context, but std::initializer_list::size() can't: std::initializer_list<int> il{1, 2, 3, 4}; constexpr std::size_t il_size = il.size(); // (1) - fails with GCC and Clang (*) std::array<int, 4> arr{1, 2, 3, 4}; constexpr std::size_t arr_size = arr.size(); // (2) - OK (*) The error is:

Create vector of tuples from two vectors by move

爱⌒轻易说出口 提交于 2021-01-27 17:48:42
问题 I want to create a std::vector of std::tuple 's ( std::vector<std::tuple<Ts...>> ) from two std::vector 's by moving the data of the std::vector s. Let's assume I have a struct similar to this (added std::cout s to showcase the problem). template<typename T> struct MyType { constexpr MyType() { std::cout << "default constructor\n"; } constexpr MyType(const T& data) : m_data(data) { std::cout << "data constructor\n"; } constexpr MyType(const MyType& other) : m_data(other.m_data) { std::cout <<

std::vector::emplace_back bug when returning references (C++17)

旧城冷巷雨未停 提交于 2021-01-27 15:31:01
问题 I've been trying to trace a bug for 10+ hours now, and by now I'm starting to think that the bug can't be on my side. However, I have a feeling it could be me who's just forgetting or misunderstanding something. I have a class member of type std::vector called temp_materials, and inside the constructor (when temp_materials is still empty), this code runs: Material &stonewallbug = temp_materials.emplace_back(resource_lib.get_shader("DeferredGeometryShader")); stonewallbug.set_texture("texture