c++14

can we do deep tie with a c++1y std::tie() -like function?

三世轮回 提交于 2019-12-03 17:19:37
Is there a way to write a variant of std::tie in c++11/1y that ties deeply into a tuple. That is, one in which tie((x,y),z) = make_tuple(make_tuple(1,2),3) binds x, y, z to 1, 2 and 3 , respectively as in the following example. It would be nice. Thanks. #include <tuple> #include <iostream> using namespace std; int main() { int x, y ,z; auto t = make_tuple(1,2); std::tie(y,x)= t; //std::tie((x,y),z) = make_tuple(t,3); //not working cout << x << y << z << endl; return 0; } Maybe you're looking for std::tuple_cat : std::tie(x,y,z) = std::tuple_cat(t, make_tuple(3)); You can string together tuples

clang error with std::unique_ptr

*爱你&永不变心* 提交于 2019-12-03 17:04:53
问题 I have base object called IList . Then I have VectorList , that inherits IList . then I have function like this: std::unique_ptr<IList> factory(){ auto vlist = std::make_unique<VectorList>(); return vlist; } This compiles without problem under gcc , but clang gives following error: test_file.cc:26:9: error: no viable conversion from 'unique_ptr<VectorList, default_delete<VectorList>>' to 'unique_ptr<IList, default_delete<IList>>' return vlist; How is proper way to handle this kind of errors?

why declare constrexpr constructors for classes with non-trivial destructors (e.g. unique_ptr, std::variant)

末鹿安然 提交于 2019-12-03 16:36:49
问题 As far as I understand (at least for c++14 ), a destructor cannot be constexpr if it is not trivial (implicit generated or =default ). What is the point of declaring constexpr constructors for structures with non-trivial destructors? struct X { int a_; constexpr X(int a) : a_{a} {} // constexpr ~X(){}; // Error dtor cannot be marked constexpr // ~X(){}; // causes error at y declaration: temporary of non-literal type ‘X’ // in a constant expression . }; template <int N> struct Y {}; int main()

Use of deleted copy constructor in the singleton

前提是你 提交于 2019-12-03 16:28:53
I've implemented the singleton pattern like this , there is my code: header file: class Settings_manager{ public: static Settings_manager& get_instance(); void operator=(Settings_manager const&) =delete; Settings_manager(Settings_manager const&) =delete; ... private: Settings_manager(); }; implementation: Settings_manager& Settings_manager::get_instance() { static Settings_manager instance; return instance; } Settings_manager::Settings_manager() { read_file(); } When I try use get_instance function in main like this: Settings_manager set = Settings_manager::get_instance(); or Settings_manager

How to construct a tuple from an array

允我心安 提交于 2019-12-03 16:27:12
I am designing a C++ library that reads a CSV file of reported data from some experiment and does some aggregation and outputs a pgfplots code. I want to make the library as generic and easy to use as possible. I also want to isolate it from the data types that are represented in the CSV file and leave the option to user to parse each column as she desires. I also want to avoid Boost Spirit Qi or other heavy duty parser. The simple solution I have is for the user to create a type for each column, with a constructor that takes "char *". The constructor does its own parsing for the value it is

Determine whether a constructor of an abstract base class is noexcept?

此生再无相见时 提交于 2019-12-03 16:27:06
问题 In C++11 and later, how to determine whether a constructor of an abstract base class is noexcept ? The following methods don't work: #include <new> #include <type_traits> #include <utility> struct Base { Base() noexcept; virtual int f() = 0; }; // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_constructible<Base>::value, ""); // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_default

How to fill an array of unique_ptr?

天大地大妈咪最大 提交于 2019-12-03 16:11:28
Is it possible to use std:fill to fill an array of unique_ptr s? The intention is to have distinct pointers to distinct objects which are initialized with the same parameters. For example: std::unique_ptr<int> ar[3]; std::fill(ar.begin(), ar.end(), make_unique_for_each_element_somehow<int>(1)); Lightness Races with Monica No, but this is what std::generate is for. Instead of being given a single value that's copied throughout the target range, std::generate is given a "generator" function that creates each value as needed. So, probably, something like this: std::unique_ptr<int> ar[3]; std:

How can I polymorphically store and access different types from the same inheritance hierarchy in contiguous memory?

ぐ巨炮叔叔 提交于 2019-12-03 16:10:37
问题 For polymorphism, the usual approach is to use std::vector<base*> . However, I have to provide the addresses myself, that is to manage the memory myself whether I use std::unique_ptr<> or raw pointers. I would like to have a polymorphic_storage<base> type that accepts any type that inherits from base . I also want the types to be stored in contiguous memory for faster traversal and for cache related concerns. However, there's a pretty big issue: In the absence of type information at the

How to check if a pointer points to a properly aligned memory location?

喜欢而已 提交于 2019-12-03 15:44:05
问题 Given a void * to some storage, how to check whether it points to properly aligned storage without any implementation defined behavior? Of course we have std::align, but is there a more effective way to do this? template <std::size_t alignment> inline bool is_aligned(void * ptr) noexcept { std::size_t max = 1u; return std::align(alignment, 1u, ptr, max); } PS: I need to do this in a C++ standards-compatible fashion, without relying on any platform-specific (implementation defined) hacks. PPS:

C++14 support in QtCreator with Clang

ⅰ亾dé卋堺 提交于 2019-12-03 15:42:28
How can I enable C++14 support in QtCreator 3.3 using Clang 3.5? I have added a Clang kit and I have added CONFIG += c++14 in my project file. However when using e.g. return type deduction I get the following error: error: 'auto' return without trailing return type; deduced return types are a C++1y extension you can use CONFIG += c++14 in .pro file with Qt5.5 but there is a bug with clang, so we need to modify the Qt/5.5/clang_64/mkspecs/features/c++14.prf file, add this code before include(c++11.prf) : contains(QMAKE_LFLAGS_CXX11, -stdlib=libc++) { QMAKE_CXXFLAGS_CXX11 += -stdlib=libc++ } I