c++14

GCC constexpr lambdas in constexpr functions and evaluation in compile time

混江龙づ霸主 提交于 2019-12-08 12:13:24
问题 Code first, we have the following piece of code that is used to accumulate a constexpr std::array in compile time: template <typename T, std::size_t N, typename O> constexpr T compile_time_accumulator(const std::array<T, N> const &A, const std::size_t i, const O& op, const T initialValue) { return (i < N) ? op(A[i], compile_time_accumulator(A, i + 1, op, initialValue)) : initialValue; } and the following code example to test/varify it (i.e., that it evaluates in compile time): constexpr std:

Saving the content of a QLineEdit object into a string variable (C++)

最后都变了- 提交于 2019-12-08 10:12:02
问题 I've looked around the Qt Documentation, but within my project, I'd like to having most of the non-graphical 'more thinking' part of my program be on a seperate .cpp file. Given that, I was wanting to take the text typed into a QLineEdit object and save it as a string after the user triggers the 'returnPressed' action, but when I type: void MainWindow::on_lineEdit_returnPressed() { QMessageBox msgBox; msgBox.setText("The entry has been modified."); msgBox.exec(); //The line which should save

Specializing class with SFINAE if a parameter pack is needed

一个人想着一个人 提交于 2019-12-08 10:10:29
问题 As I got a perfect answer for the question: Specializing class with SFINAE For completeness I insert the correct solution as example here again: class AA { public: using TRAIT = int; }; class BB { public: using TRAIT = float; }; template < typename T, typename UNUSED = void> class X; template < typename T > class X<T, typename std::enable_if< std::is_same< int, typename T::TRAIT>::value, void >::type> { public: X() { std::cout << "First" << std::endl; } }; template < typename T > class X<T,

Searching through a tuple for arguments of a function

被刻印的时光 ゝ 提交于 2019-12-08 07:39:47
问题 Consider this int foo (int a, char c, bool b) {std::cout << a << ' ' << c << ' ' << b << '\n'; return 8;} double bar (int a, char c, bool b, int d) {std::cout << a << ' ' << c << ' ' << b << ' ' << d << '\n'; return 2.5;} char baz (bool a, bool b) {std::cout << a << ' ' << b << '\n'; return 'a';} int main() { const auto tuple = std::make_tuple(5, true, 'a', 3.5, false, 1000, 't', 2, true, 5.8); const std::tuple<int, double, char> t = searchArguments (tuple, foo, bar, baz); } So the arguments

How to get the real calendar microseconds time (epoch since 1970) in Mac OSX?

。_饼干妹妹 提交于 2019-12-08 07:37:54
问题 Kindly go through below Qn for the context: Why does clang++/g++ not giving correct microseconds output for chrono::high_resolution_clock::now() in Mac OSX? As already discussed in above thread, I intend to get microseconds time since 1970. Now using chrono::high_resolution_clock::now().time_since_epoch() works well in popular platforms except OSX & possibly iOS. In [our] Mac systems, the microseconds time is generated since the system restart & not since 1970. Is there any portable [or Mac

std::atomic trivially copyable structs

徘徊边缘 提交于 2019-12-08 05:55:22
问题 C++ reference says: http://en.cppreference.com/w/cpp/atomic/atomic std::atomic may be instantiated with any TriviallyCopyable type T However following example does not work under g++ 6.2.0 #include <atomic> #include <functional> struct Test11 { int x; }; struct Test12 { char x; }; struct Test13 { long x; }; struct Test2 { char x; int y; }; struct Test3 { int y; long x; }; template<typename T, typename... ARGS> void test(ARGS&& ... args) { static_assert(std::is_trivially_copyable<T>::value);

Variadic construction for initialising vector of unique_ptr to base type

点点圈 提交于 2019-12-08 05:50:43
问题 Below is an example program where a 'Container' class needs to store a list of 'Items' via a base class pointer. Having started with C++11/14 the natural choice would be to use std::unique_ptr and variadic templates in the case I have. However being new I cannot fathom how to convert the variadic list into and initialiser list for the vector of unique_ptr in a manner that compiles. Help is greatly appreciated as I have failed to find anything online that helps me with this issue so far

Static template member function for template class

孤街浪徒 提交于 2019-12-08 05:44:26
问题 I have a template class and a template member function: template<class T1> struct A{ template<class T2> static int f(){return 0;} }; I want to specialize for a case when T1 and T2 are the same, For example, define the case A<T>::f<T> for any T . but I can't find the combination of keywords to achieve this. How can I partially (?) specialize a combination of template class and a template static function? These are my unsuccessful attempts, and the error messages: 1) Specialize inside the class

ODR of template class with static constexpr member

本小妞迷上赌 提交于 2019-12-08 05:38:11
问题 I know, there are many answered question about linkage of a static (constexpr) members. But I wonder, why using a template class out-of-line definition works in a header file but not for a specialized class. a) This works without linker error: template<typename, typename> struct Foobar; template<typename T> struct Foobar<int, T> { static constexpr std::array<int, 1> a = {{1}}; }; template<typename T> constexpr std::array<int, 1> Foobar<int, T>::a; // foo.cpp std::cout << Foobar<int, int>::a[0

Prevent the creation of temporary objects

依然范特西╮ 提交于 2019-12-08 05:33:32
问题 I have the same question that was asked over here five years ago: Disallowing creation of the temporary objects The reason I am asking it again is that I am specifically interested in C++11 and C++14 techniques for preventing the construction of temporaries. One idea is to use "rvalue reference for *this": class ScopedLock { public: void enable() && = delete; void enable() & { is_enabled = true; } ~ScopedLock() { if (!is_enabled) { std::cerr << "ScopedLock misuse." << std::endl; std: