c++14

C++ Template Meta Programming: Different Behavior using Types Aliases vs Inheritance

时光总嘲笑我的痴心妄想 提交于 2019-12-11 04:55:25
问题 I was trying to reverse a c++14 std::index_sequence and ran into problems with my original implementation that used inheritance. I found a workaround using local type aliases, but I would like to understand why the original code does not work. Broken Reverse Using Inheritance This was my first attempt at reversing a std::index_sequence : /// Helper class that appends an element onto an index_sequence. /// Base case. template<size_t, typename> struct Append : std::index_sequence<> { };

C++ : Handle thread-local object destruction

谁说我不能喝 提交于 2019-12-11 04:06:22
问题 I have a logging system, which basically uses a thread-local buffer to log. This helps in reducing locking. A bunch of message can be written into the thread-local buffer and flushed in one shot. And also since it is thread-local, we can avoid allocating buffer per log message. Anyway the issue is during the process exit. We are seeing crash while accessing the thread-local buffer. The thread-local object I have is something like std::vector<Buffer> . [ vector because there are multiple

Getting nth variadic argument value (not type)

痴心易碎 提交于 2019-12-11 03:47:01
问题 Ignore the missing perfect forwarding. (Assume arguments are perfectly forwarded in the real implementation.) // Base case: no args template<typename TF> void forEach2Args(TF) { } // Recursive case: some args template<typename TF, typename... Ts> void forEach2Args(TF mFn, Ts... mXs) { mFn(getNth<0>(mXs...), getNth<1>(mXs...)); forEach2Args(mFn, getAllAfter<2>(mXs...)); } int main() { int result{0}; forEach2Args([&result](auto a1, auto a2) { result += (a1 * a2); }, 2, 4, 3, 6); // roughly

Completely enumerate indices of D-dimensional array at compile time

对着背影说爱祢 提交于 2019-12-11 03:13:37
问题 To test some multidimensional structures there is a need to generate compile time multidimensional indices to fully cover all the possible cases. I search for compile-time inexpensive way to achieve above purpose. What I do currently: #include <type_traits> #include <utility> template< typename F, std::size_t ...indices > struct enumerator; template< typename F > struct enumerator< F > { constexpr enumerator(F && _f) : f(std::forward< F >(_f)) { ; } template< std::size_t ...I > constexpr bool

std::function variable arguments in one vector/map

北战南征 提交于 2019-12-11 02:54:29
问题 how could one do this in c++ today without using two separate holders? typedef std::function<void(int a, int b)> f1; typedef std::function<void(int a)> f2; std::vector<f1> m; void add(f1 f) { m.push_back(f); } void add(f2 f) { // add one more (unused) parameter to f2 so we can add f2 to f1 vector holder? } can we somehow overload f1 function to include different set of parameters? could this be solved by variadic templates nowdays or something similar? 回答1: Create a new lambda matching the

Is Object Editor a good approach if there are multiple member functions to call?

妖精的绣舞 提交于 2019-12-11 02:28:46
问题 I am often annoyed by sequential calls of class member function like this (ignore new usage, it's for Qt, but it's not strictly Qt-related) A a = new A(); a->fun1("one"); a->fun2(1, 2); ... a->fun10("end"); I always felt that such code should be written as simple instruction, not a project dominating lines. Simple example from Qt: auto* spinBox = new QSpinBox(); spinBox->setRange(-100, 100); spinBox->setValue(50); spinBox->setSingleStep(5); newLayout->addWidget(spinBox); But I would prefer to

Omit template arguments when create an instance of template class from another instance of template class

白昼怎懂夜的黑 提交于 2019-12-11 01:49:11
问题 I want to omit some template parameter T1,T2 when create an instance of a class DeriveGenerator<T3,T4,T1,T2> to comfort my life. Here is the ultimately simplified version of what I am encountering. My library:- The important part is the class declaration. ( this line ) Their internal content is just a filler. template<class T1,class T2>class BaseGenerator{ //<-- this line public: std::pair<T1*,T2*> generateBase(){ /** actually create T3,T4 internally */ return std::pair<T1*,T2*>(nullptr

How to store pointer to function template which takes Callable object as one of its parameters

柔情痞子 提交于 2019-12-11 01:29:16
问题 Consider following example: template <typename T> void f (T t) { std::cout << t << std::endl; } template <typename T> struct F { static constexpr void (*m) (T) = &f; }; and usage: F<int>::m (10); So far, so good. Problem shows up when I want to store pointer to function template which takes for example an lambda expression. Consider this one: template <typename T, typename C> void g (T t, C c) { std::cout << c (t) << std::endl; } template <typename T, typename C> struct G { static constexpr

use the signature of a generic function dependent on a non-type template argument as template template argument

故事扮演 提交于 2019-12-11 00:34:54
问题 The small program below, which compiles and runs, allows to bridge a runtime variable index of type unsigned with a set of template functions having one template argument J of type unsigned . In case further clarifications are needed, this is better explained in this question. The auxiliary functions I wrote use a template template argument, to infer as much info as possible from the original function. The problem is that I could not find a better way to define the template template argument

Complete object or subobject?

不羁的心 提交于 2019-12-10 23:56:16
问题 The C++14 Standard says: A subobject can be a member subobject, a base class subobject, or an array element. An object that is not subobject of any other object is called a complete object. (§1.8(2)) It is not obvious to me whether the 'can be' is meant as an implicit 'if and only if'. To provide an example, in the snippet below, is r a reference to a complete object or to a subobject? #include <iostream> int main(){ int i=2; unsigned char & r=reinterpret_cast<unsigned char&>(i); std::cout<<