c++14

Is order in memory guaranteed for class private members in C++?

守給你的承諾、 提交于 2019-12-11 15:34:51
问题 class my_class_t { private: uint64_t field1; uint64_t field2; }; Is order of field1 and field2 guaranteed in memory by C++ Standard? UPD. Answers said that field2 it is, but &field2 may be not equal to &field1 + 1 . How to ensure that field2 will be immediately after field1 ? 回答1: They are guaranteed to have increasing addresses with respect to each other ([class.mem]/13): Nonstatic data members of a (non-union) class with the same access control (Clause [class.access]) are allocated so that

C++17 construct array in stack using chosen constructor (same constructor parameter values for each array entry)

痞子三分冷 提交于 2019-12-11 14:31:31
问题 Is it a way in c++17 to construct an array in stack using another constructor than the default constructor. This is a special case when each array value is constructed with the same constructor parameters. I need to use a basic stack located array (not a vector or an array of pointers or something else). This code illustrate what I would like to do : #include <iostream> using namespace std; struct A { A() { printf("A() called\n"); } A(int i, int j) { printf("A(%d, %d) called\n", i, j); } };

QSqlDatabase: QMYSQL driver not loaded on Xubuntu 16.04 64bits

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 12:42:35
问题 I'm trying to follow the answer given in QSqlDatabase: QMYSQL driver not loaded on Ubuntu 15.04 64bits on Xubuntu 16.04, where: # ls /usr/lib/x86_64-linux-gnu/libmysql* -ls 5624 -rw-r--r-- 1 root root 5757198 Abr 21 10:30 /usr/lib/x86_64-linux-gnu/libmysqlclient.a 0 lrwxrwxrwx 1 root root 20 Abr 21 10:29 /usr/lib/x86_64-linux-gnu/libmysqlclient.so -> libmysqlclient.so.20 0 lrwxrwxrwx 1 root root 24 Abr 21 10:29 /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20 -> libmysqlclient.so.20.2.1 4140

How to pass other template parameter when template class uses parameter pack?

為{幸葍}努か 提交于 2019-12-11 12:12:36
问题 I would like to create template class that implements print() method for each type passed as template parameters. Something like that: class Interface { public: virtual ~Interface() = default; virtual void print(int) = 0; virtual void print(double) = 0; }; X x<int, double, Interface>; class X has public method void print() and it works. The whole code below: #include <iostream> #include <type_traits> struct Printer { void print(int i) {std::cout << i << std::endl; } void print(double d) {std:

Can I use the iterator Libraries' Access Functions on Nonstandard Containers?

一笑奈何 提交于 2019-12-11 11:43:30
问题 The iterator library has been introducing a lot of access functions over the course of C++11, C++14, and C++17: begin / end cbegin / cend crbegin / crend data empty rbegin / rend size Can I use these on any container, even nonstandard containers (provided they supply an accessible corresponding method?) For example given a QVector foo can I do this: const auto bar = begin(foo); 回答1: The declarations for std::begin are as follow (from §24.7): template <class C> auto begin(C& c) -> decltype(c

writable zip ranges are not possible?

那年仲夏 提交于 2019-12-11 11:05:08
问题 The following is failing: #include <range/v3/view.hpp> #include <range/v3/view/zip.hpp> #include <range/v3/utility/iterator.hpp> // ... std::vector< std::tuple<int, std::string> > const data{ {1,"a"}, {2,"b"}, {3,"c"} }; std::vector<int> vi(data.size()); std::vector<std::string> vs(data.size()); using namespace ranges; copy(data, view::zip(vi,vs) ); // error clang says No matching function for call to object of type 'const ranges::v3::with_braced_init_args<ranges::v3::copy_fn>' Assuming this

C++ implicit numeric type demoting

北城以北 提交于 2019-12-11 10:53:27
问题 Recently, I have noticed that C/C++ seems to be very permissible with numeric type conversion, as it implicitly casts a double to int. Test: Environment: cpp.sh , Standard C++ 14 , Compilation warnings all set Code: int intForcingFunc(double d) { return d; // this is allowed } int main() { double d = 3.1415; double result = intForcingFunc(d); printf("intForcingFunc result = %f\n", result); int localRes = d; // this is allowed printf("Local result = %d\n", localRes); int staticCastRes = static

Portable floating point variable templates

瘦欲@ 提交于 2019-12-11 10:47:08
问题 How to define portable high-precision floating point variable templates in c++14? The program below should print pi with double and long double precision. #include <iostream> #include <iomanip> #include <limits> template<typename T> constexpr T pi = T(3.141592653589793238462643383279502884197); int main() { std::cout << std::setprecision(std::numeric_limits<double>::max_digits10) << pi<double> << std::endl; std::cout << std::setprecision(std::numeric_limits<long double>::max_digits10) << pi

Using `std::conditional_t` to define a class' `typedef` in dependence of its template parameter

怎甘沉沦 提交于 2019-12-11 08:39:19
问题 I try to use std::conditional_t to define a class A 's typedef in dependence of its template parameter T : template< typename T > class A { public: typedef std::conditional_t< std::is_fundamental<T>::value, T, decltype(std::declval<T>().foo())> type; }; template< typename T > class B { public: T foo() { // ... } }; int main() { typename A< int >::type a = 5; // causes an error typename A< B<int> >::type b = 5; // does not cause an error return 0; } Unfortunately, the code does not compile.

boost::any library doesn't compile: “Array used as initializer” error

﹥>﹥吖頭↗ 提交于 2019-12-11 07:59:57
问题 I'm using boost::any (among other Boost functionalities) in a C++ project. The following compiles just fine on my Mac (MacBook Pro Retina running the latest version of Mavericks) with g++: #include <boost/any.hpp> but when I use Ubuntu Linux with g++ using the same compilation settings / flags, I get the following error: In file included from /home/alexandergunnarson/Documents/Source Code/byu/library/collections.cpp:11:0, from /home/alexandergunnarson/Documents/Source Code/byu/library