Skip to content

joboccara/NamedType

NOTE:

在阅读 fluentcpp Variadic CRTP: An Opt-in for Class Features, at Compile Time 时,发现的它;

A strong type is a type used in place of another type to carry specific meaning through its name.

NOTE: 对strong type的解释是非常准确的

using Meter = NamedType<double, MeterTag, Addable, Printable>

embeddedartistry NamedType: The Easy Way to Use Strong Types in C++

Read code

NOTE: 它的实现点有:

一、CRTP

二、用了两次CRTP:

1、NamedType/include/NamedType/crtp.hpp

每个skill继承它来获得 underlying() 接口

2、NamedType/include/NamedType/named_type_impl.hpp NamedType

可以继承多个skill来获得skill。

三、policy based design

四、mixin from above

NamedType/include/NamedType/named_type_impl.hpp

template <typename T, typename Parameter, template <typename> class... Skills>
class FLUENT_EBCO NamedType : public Skills<NamedType<T, Parameter, Skills...>>...
{

};

NOTE:

using Meter = NamedType<double, MeterTag, Addable, Printable>

Skill只有一个template parameter,它的template parameter是一个template,这个template接收一个template parameter: NamedType

NamedType/include/NamedType/crtp.hpp

#ifndef CRTP_HPP
#define CRTP_HPP

namespace fluent
{

template <typename T, template <typename> class crtpType>
struct crtp
{
    constexpr T& underlying()
    {
        return static_cast<T&>(*this);
    }
    constexpr T const& underlying() const
    {
        return static_cast<T const&>(*this);
    }
};

} // namespace fluent

#endif-

NOTE: 思考: template parameter crtpType 有什么用?

NamedType/include/NamedType/underlying_functionalities.hpp

namespace fluent
{

template <typename T>
struct PreIncrementable : crtp<T, PreIncrementable>
{
    IGNORE_SHOULD_RETURN_REFERENCE_TO_THIS_BEGIN

    FLUENT_CONSTEXPR17 T& operator++()
    {
        ++this->underlying().get();
        return this->underlying();
    }

    IGNORE_SHOULD_RETURN_REFERENCE_TO_THIS_END
};

}