c++14

How to deduce the size of a compile time a const char string with C++ templates?

百般思念 提交于 2021-02-08 11:30:48
问题 I am trying to use clang++ to expand the template code from this post. Here is what I come up with. constexpr unsigned int requires_inRange(unsigned int i, unsigned int len) { return i >= len ? throw i : i; } class StrWrap { unsigned size_; char * const begin_; public: template< unsigned N > constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) { static_assert( N >= 1, "not a string literal"); } constexpr char operator[]( unsigned i ) { return requires_inRange(i, size_), begin_

How to deduce the size of a compile time a const char string with C++ templates?

不打扰是莪最后的温柔 提交于 2021-02-08 11:30:25
问题 I am trying to use clang++ to expand the template code from this post. Here is what I come up with. constexpr unsigned int requires_inRange(unsigned int i, unsigned int len) { return i >= len ? throw i : i; } class StrWrap { unsigned size_; char * const begin_; public: template< unsigned N > constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) { static_assert( N >= 1, "not a string literal"); } constexpr char operator[]( unsigned i ) { return requires_inRange(i, size_), begin_

initialize double nested std::array from variadic template array reference constructor

佐手、 提交于 2021-02-08 11:30:25
问题 Problem I have a Matrix class that is able to do some math. It holds its data in a double nested std::array as a variable. I have a constructor that takes an array reference as a variadic template. I did this so i could add some SFINAE more easily (omitted here). #include <array> template <std::size_t N, std::size_t M, typename T> class Matrix{ public: template <typename... TArgs> Matrix(TArgs const(&&... rows)[M]) { // ?? } // ... private: std::array<std::array<T,M>, N> data; }; Question How

format date/time value shown by a QTableView

北战南征 提交于 2021-02-08 11:14:09
问题 I´m using a QTableView to show a database table via model. One of the table columns have a timestamp, acctually a QDateTime was stored there before. Is there some way to format the timestamp value at presentation time? I was thinking in something like the .toString of a QDateTime("yyyy-MM-dd hh:mm:ss.zzz") . 回答1: It is possible to return date formatted as you wish in this virtual method of QAbstractItemModel : QVariant QAbstractItemModel::data(const QModelIndex &item, int role = Qt:

format date/time value shown by a QTableView

人走茶凉 提交于 2021-02-08 11:13:02
问题 I´m using a QTableView to show a database table via model. One of the table columns have a timestamp, acctually a QDateTime was stored there before. Is there some way to format the timestamp value at presentation time? I was thinking in something like the .toString of a QDateTime("yyyy-MM-dd hh:mm:ss.zzz") . 回答1: It is possible to return date formatted as you wish in this virtual method of QAbstractItemModel : QVariant QAbstractItemModel::data(const QModelIndex &item, int role = Qt:

Is it possible / desirable to create non-copyable shared pointer analogue (to enable weak_ptr tracking / borrow-type semantics)?

心不动则不痛 提交于 2021-02-08 09:07:20
问题 Problem: Unique_ptrs express ownership well, but cannot have their object lifetimes tracked by weak_ptrs. Shared_ptrs can be tracked by weak_ptrs but do not express ownership clearly. Proposed solution: Derive a new pointer type (I'm going to call it strong_ptr) that is simply a shared_ptr but with the copy constructor and assignment operator deleted, so that it is hard to clone them. We then create another new borrowed_ptr type (which is not easily storable) to handle the temporary lifetime

HTTP Requests in C++ without external libraries? [closed]

拟墨画扇 提交于 2021-02-08 08:16:07
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Improve this question So this question has been asked before, but the general answer pointed to was using an external library such as cURLpp. So I was curious as to if HTTP requests could be done using only the standard libraries as of C++14. How difficult would this be? Say for

Passing result of std::bind to std::function “overloads”

微笑、不失礼 提交于 2021-02-08 05:17:42
问题 I have problem similar to Passing different lambdas to function template in c++ but now with wrappers created by std::bind instead of lambdas. I have two overloads of method Add that take different forms of std::function : template<typename T> struct Value { T value; }; template <typename T> void Add(Value<T> &value, function<bool()> predicate) { } template <typename T> void Add(Value<T> &value, block_deduction<function<bool(const Value<T> &)>> predicate) { } This now works fine with lambdas

C++ constexpr in place aligned storage construction

为君一笑 提交于 2021-02-08 02:11:34
问题 I'm trying to make an aligned variant type that uses std::aligned_storage to hold the data. Is there a way to construct an object in place in a constexpr way? I read you can't do constexpr placement new. #include <iostream> #include <string> struct foo { foo(std::string a, float b) : bar1(a), bar2(b) {} std::string bar1; float bar2; }; struct aligned_foo { template<typename... Args> aligned_foo(Args&&... args) { //How to constexpr construct foo? data_ptr = ::new((void*)::std::addressof

C++ constexpr in place aligned storage construction

人盡茶涼 提交于 2021-02-08 02:06:18
问题 I'm trying to make an aligned variant type that uses std::aligned_storage to hold the data. Is there a way to construct an object in place in a constexpr way? I read you can't do constexpr placement new. #include <iostream> #include <string> struct foo { foo(std::string a, float b) : bar1(a), bar2(b) {} std::string bar1; float bar2; }; struct aligned_foo { template<typename... Args> aligned_foo(Args&&... args) { //How to constexpr construct foo? data_ptr = ::new((void*)::std::addressof