Posts

Showing posts with the label templates

Derive a templated class' member variable type from a template member type

10 The title might seem a little confusing, so here is a more thorough explanation: I have a templated class that has a vector as a member variable. The template argument is a struct (or class) that will have one certain variable. The type of this vector shall be derived from the template argument (from this one certain variable). The tricky part is that it shall be derived from a member variable of the template argument. #include <vector> #include <complex> using namespace std; struct thingA { double variable; //... }; struct thingB { complex<double> variable; //... }; template <class S> class someClass { vector< " type of S::variable " > history; // If S=thingA, make it a double, if S=tingB make it a co...

How to reorder function parameters?

9 0 Look at the pseudo-c++ code below: typedef *** SomeType1; typedef *** SomeType2; typedef *** SomeType3; void BFunc(SomeType1& st1, SomeType2& st2, SomeType3& st3) { /*some work*/; } template <typename T1, typename T2, typename T3> void AFunc(T1& p1, T2& p2, T3& p3) { BFunc(???); } There are two functions with parameters. The parameters count larger than three, but for simplicity for example let it would be three. The Afunc - it is the templated function that have the same parameters count as the BFunc plus the parameters have the same types as the BFunc parameters. But (!) the sequence on the parameters of BFunc can (or cannot) be different. For example: BFunc(int, double, char) AFunc<double, int, char> AFunc...

C++ template deduction from lambda

I have a function which takes two std::functions as arguments. The parameter of the second function has the same type as the result of the first. I wrote a function template like this: template<typename ResultType> void examplFunction(std::function<ResultType()> func, std::function<void(ResultType)> func2) { auto x = func(); func2(x); } I can call it with: void f() { examplFunction<int>([]() { return 1; }, // [](int v) { std::cout << "result is " << v << std::endl; }); } Is there a way to to get rid of the <int> at examplFunction<int> and let the compiler deduce the type of ResultType? Do you actually need std::function in there? std::function is useful when you need type erasure. With templates, you can usually skip it altogether: template<class F1, class F2> void examplFunction(F1 func, F2 func2, decltype(func2(func()))* sfinae = nullptr) { auto x = func(); func2(x); } The sfi...

Pimpl - Why can make_unique be called on an incomplete type

Why does the make_unique call compile? Doesn't make_unqiue require its template argument to be a complete type ? struct F; int main() { std::make_unique<F>(); } struct F {}; The question orignated from my "problem" with my PIMPL implementation: I do understand why the destructor has to be user declared and defined inside the cpp file for the Implementation class (PIMPL). But moving the constructor of the class containing the pimpl- still compiles. class Object {}; class CachedObjectFactory { public: CachedObjectFactory(); ~CachedObjectFactory(); std::shared_ptr<Object> create(int id) const; private: struct CacheImpl; std::unique_ptr<CacheImpl> pImpl; }; Now the cpp file: // constructor with make_unique on incompete type ? CachedObjectFactory::CachedObjectFactory() : pImpl(std::make_unique<CacheImpl>()) {} struct CachedObjectFactory::CacheImpl { std::map<int, std::shared_ptr<Object>> idToObjects; }; //deferred destructor ...

Make a function accepting an optional to accept a non-optional?

I'm trying to write syntactic sugar, in a monad-style, over std::optional. Please consider: template<class T> void f(std::optional<T>) {} As is, this function cannot be called with a non-optional T1 (e.g. an int), even though there exists a conversion from T to std::optional<T>2. Is there a way to make f accept an std::optional<T> or a T (converted to an optional at the caller site), without defining an overload3? 1) f(0): error: no matching function for call to 'f(int)' and note: template argument deduction/substitution failed, (demo). 2) Because template argument deduction doesn't consider conversions. 3) Overloading is an acceptable solution for a unary function, but starts to be an annoyance when you have binary functions like operator+(optional, optional), and is a pain for ternary, 4-ary, etc. functions. Another version. This one doesn't involve anything: template <typename T> void f(T&& t) { std::opt...