static-polymorphism

Inheriting from a template class set of operators using CRTP

社会主义新天地 提交于 2021-02-11 12:40:35
问题 This is a follow-up from this Q/A. I'm in the process of taking user Jarod42's advice by using template <template<typename> class C, typename T> instead of the example that I have shown as my answer to the question. My code currently looks like this: template<template<typename> class C, typename T> struct single_member_ops { friend auto operator+(const C<T>& lhs, const C<T>& rhs) { return lhs.value + rhs.value; } friend auto operator+(const C<T>& lhs, const T& rhs) { return lhs.value + rhs;}

Inheriting from a template class set of operators using CRTP

家住魔仙堡 提交于 2021-02-11 12:39:12
问题 This is a follow-up from this Q/A. I'm in the process of taking user Jarod42's advice by using template <template<typename> class C, typename T> instead of the example that I have shown as my answer to the question. My code currently looks like this: template<template<typename> class C, typename T> struct single_member_ops { friend auto operator+(const C<T>& lhs, const C<T>& rhs) { return lhs.value + rhs.value; } friend auto operator+(const C<T>& lhs, const T& rhs) { return lhs.value + rhs;}

Static duck typing in C++

眉间皱痕 提交于 2021-02-07 02:39:56
问题 C++ has some sort of duck typing for types given by template parameters. We have no idea what type DUCK1 and DUCK2 will be, but as long as they can quack() , it will compile and run: template <class DUCK1, class DUCK2> void let_them_quack(DUCK1* donald, DUCK2* daisy){ donald->quack(); daisy->quack(); } But it's a bit inconvenient to write. When I do absolutely not care what actual types DUCK1 and DUCK2 are but rather want to fully use the idea of duck typing, then I would like to have

C++ static rather than dynamic polymorphism

回眸只為那壹抹淺笑 提交于 2019-12-21 17:23:43
问题 I'm trying to build a generic algorithm. So far I have achieved this using class hierarchy and pointers, as in the example below: struct Base{ virtual double fn(double x){return 0;} }; class Derived : public Base{ double A; public: Derived(double a) : A(a) {} double fn(double x) { return A*x;} }; //Some other implementations class algo{ double T; std::unique_ptr<Base> b_ptr; public: algo(double t, std::unique_ptr<Base>& _ptr); //move constructor... //Some constructors double method(double x){

Static polymorphism definition and implementation [closed]

孤街浪徒 提交于 2019-12-17 09:21:17
问题 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 last year . I have some questions about the concept of static polymporhism I somethimes hear about; you may interpret them primarily in the context of C++, but I'd appreciate language-agnostic answers where applicable ( hence tagging both C++ and language-agnostic ). How do we define static

Static polymorphism definition and implementation [closed]

旧时模样 提交于 2019-12-17 09:21:14
问题 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 last year . I have some questions about the concept of static polymporhism I somethimes hear about; you may interpret them primarily in the context of C++, but I'd appreciate language-agnostic answers where applicable ( hence tagging both C++ and language-agnostic ). How do we define static

Finding base class at compile time

狂风中的少年 提交于 2019-12-07 06:58:16
问题 The title almost says everything: Is there a way in C++ to get a class's base type(s) at compile time? I. e. is it possible to hand a class to a template, and let the template use other templates to which it hands the bases of the given class? My question is not whether I can implement such a functionality myself, there is no question I can (using traits and the like). My question is whether there is some (obscure) builtin functionality that could be used to this end. 回答1: gcc supports this.

Curiously Recurring Template Pattern and statics in the base class

大憨熊 提交于 2019-12-06 20:46:35
So thanks to this answer I'm looking at implementing my problem with CRTP. However I have a problem. In my static base class I have 2 sets of functions. One takes std::vectors and one takes a standard C-style array. So in the base class I define a static function that calls the non-std::vector function. However when I derive from that base class I seem to no longer be able to access the public static function in the base class (Which I thought I could). template< class Derived > class Base { public: static void Func( std::vector< float >& buffer ) { Func( &buffer.front(), buffer.size() ); }

why no need of forward declaration in static dispatching via templates?

你。 提交于 2019-12-04 16:29:32
问题 I am playing a bit with static polymorphism, I'm calling a function which internally calls the "right" specialized function depending on the type of the initial argument (basically I'm doing tagging). Here is the code: #include <iostream> using namespace std; // tags struct tag1{}; struct tag2{}; // the compliant types, all should typedef tag_type struct my_type1 { using tag_type = tag1; }; struct my_type2 { using tag_type = tag2; }; // static dispatch via tagging template <typename T> void f

Is there real static polymorphism in C++?

眉间皱痕 提交于 2019-12-04 09:13:24
问题 Here is a simple code in C++: #include <iostream> #include <typeinfo> template<typename T> void function() { std::cout << typeid(T).name() << std::endl; } int main() { function<int>(); function<double>(); return 0; } I have read that templates in C++ is a compile-time feature, which is not like generics in C#/Java. So as I understood, the C++ compiler will divide a single defined function into various number (depends on calls count with different type) of functions. Am I right or not? I'm not