crtp

How to get generic type definition for CRTP type

瘦欲@ 提交于 2019-12-30 18:54:13
问题 Given the following CRTP type in C#: public abstract class DataProviderBase<TProvider> where TProvider : DataProviderBase<TProvider> { } How would I get its generic type definition in F#? let typeDef = typedefof<DataProviderBase<_>> yields the error: Type constraint mismatch when applying the default type 'DataProviderBase<'a>' for a type inference variable. The resulting type would be infinite when unifying ''a' and 'DataProviderBase<'a>' Consider adding further type constraints In C#, it

Reflexive type parameter constraints: X<T> where T : X<T> ‒ any simpler alternatives?

守給你的承諾、 提交于 2019-12-30 00:54:11
问题 Every so often I am making a simple interface more complicated by adding a self-referencing ("reflexive") type parameter constraint to it. For example, I might turn this: interface ICloneable { ICloneable Clone(); } class Sheep : ICloneable { ICloneable Clone() { … } } //^^^^^^^^^^ Sheep dolly = new Sheep().Clone() as Sheep; //^^^^^^^^ into: interface ICloneable<TImpl> where TImpl : ICloneable<TImpl> { TImpl Clone(); } class Sheep : ICloneable<Sheep> { Sheep Clone() { … } } //^^^^^ Sheep

Initializing a static constexpr data member of the base class by using a static constexpr data member of the derived class

浪尽此生 提交于 2019-12-29 06:43:14
问题 Consider the following code: template<typename T> struct S { static constexpr int bar = T::foo; }; struct U: S<U> { static constexpr int foo = 42; }; int main() { } GCC v6.1 compiles it, clang 3.8 rejects it with the error: 2 : error: no member named 'foo' in 'U' struct S { static constexpr int bar = T::foo; }; Which compiler is right? Could it be due to the fact that U is not a complete type at the point where we try to use it within S ? In this case, it should be considered a bug of GCC,

Design alternative for access to derived class member from base class pointer

拜拜、爱过 提交于 2019-12-25 07:47:45
问题 I'm writing a DAL/ORM library. This library will be accessed mainly from GUIs but also from some "business level" applications. I'm still in the design phase of this library and came to a point where I'm not sure how to solve the following issue nicely. In my current design I have a class, let's call it List for the moment, that has a container of another class, Properties . Properties come in two flavors (A and B), with mostly the same functionality, but some of their functionality is

What is the cause of this overload resolution headache?

淺唱寂寞╮ 提交于 2019-12-25 05:38:35
问题 I've got a program where I've got a lot of nested if/switch statements which were repeated in several places. I tried to extract that out and put the switches in a template method class, and then allow clients to overload which switch branches they wanted to specifically handle using overloading: class TraitsA {}; class TraitsB : public TraitsA {}; class Foo { bool traitsB; public: // Whether or not a Foo has traitsB is determined at runtime. It is a // function of the input to the program

CRTP and unique persistent identifiers

▼魔方 西西 提交于 2019-12-24 14:53:08
问题 Consider the following code: #include <iostream> #include <cstdlib> #include <ctime> struct BaseClass { static int identifier() { static int identifier_counter = 0; return identifier_counter++; } }; template <class D> struct Class: public BaseClass { static int identifier() { static int class_identifier = BaseClass::identifier(); return class_identifier; } }; struct A: public Class<A> { }; struct B: public Class<B> { }; int main() { std::srand(std::time(0)); int r = std::rand()%2; if(r) { std

Securing CRTP: is private destructor the only solution?

丶灬走出姿态 提交于 2019-12-23 13:12:04
问题 How to avoid template <typename Derived> struct base { int foo() { return static_cast<Derived*>(this)->bar(); } }; struct derived : base<derived> { int bar(); }; struct another_derived : base<derived> { int bar(); }; // error: wrong base without additional code in derived classes ? This has been asked twice before (though without the extra condition of avoiding additional code in derived classes), with the recommended answer template <typename Derived> struct base { int foo() { return static

Securing CRTP: is private destructor the only solution?

拟墨画扇 提交于 2019-12-23 13:09:24
问题 How to avoid template <typename Derived> struct base { int foo() { return static_cast<Derived*>(this)->bar(); } }; struct derived : base<derived> { int bar(); }; struct another_derived : base<derived> { int bar(); }; // error: wrong base without additional code in derived classes ? This has been asked twice before (though without the extra condition of avoiding additional code in derived classes), with the recommended answer template <typename Derived> struct base { int foo() { return static

Iterator Inheritance and inheriting *this

青春壹個敷衍的年華 提交于 2019-12-23 06:14:50
问题 How to write a base class and several derived classes of iterator? Does the iterator have to return itself (*this)? So far, I use typename X and static_cast<X&>(*this) to allow the derived class to inherit a function that return itself from the base class. This looks ugly. Is there a better way? Simplified Code: #include <iterator> #include <iostream> template <typename T, typename X> class BaseIterator : public std::iterator<std::input_iterator_tag, T> { //Not intended to be used directly.

how to cast to CRTP in java?

孤街浪徒 提交于 2019-12-22 22:50:10
问题 I have a pretty simple case where I do some basic generic assignment: final Detail detail = field.getAnnotation(Detail.class); final String example = detail.example(); final Class<?> type = field.getType(); if (List.class.isAssignableFrom(type)) ... else if (Enum.class.isAssignableFrom(type)) setValue(contract, field, Enum.valueOf(type, example)); else if (...) ..... but the Enum.valueOf() is a bit difficult to call, in my case, the error is: valueOf(java.lang.Class,java.lang.String) in java