如果它存在为T :: Zero,如何生成使用自定义零值初始化std :: vector的代码?

kfm*_*e04 2 c++ structure sfinae type-traits c++11

背景

我有一个容器类,它有一个std::vector<T>成员,我用构造函数初始化size_t n_items.我想用我自己的Zero()函数初始化该向量,returns 0;默认情况下,但如果T::Zero存在静态成员,我想要返回它.

在我的第一次尝试中,我使用表达式SFINAE,但由于模糊的重载而失败,因为零和零的通用版本具有相同的签名而没有参数.所以现在,我正在尝试将代码转换为类operator().

我想我需要以std::enable_if某种方式使用,但我不知道如何编写这个.

失败的尝试

#include <cassert>
#include <iostream>
#include <vector>

template<typename T>
struct Zero
{
        T operator() const { return 0; }
};

template<typename T>
struct Zero
{
        auto operator() const ->
                decltype( T::Zero )
        {
                return T::Zero;
        }
};

struct Foo
{
        char m_c;
        static Foo Zero;

        Foo() : m_c( 'a' ) { }
        Foo( char c ) : m_c( c ) { }
        bool operator==( Foo const& rhs ) const { return m_c==rhs.m_c; }
        friend std::ostream& operator<<( std::ostream& os, Foo const& rhs )
        {
                os << (char)(rhs.m_c);
                return os;
        }
};

Foo Foo::Zero( 'z' );

int
main( int argc, char** argv )
{
        std::vector<unsigned>  v( 5, Zero<unsigned>() );
        std::vector<Foo>       w( 3, Zero<Foo>()      );

        for( auto& x : v )
                std::cout << x << "\n";
        std::cout << "---------------------------------\n";
        for( auto& x : w )
        {
                assert( x==Foo::Zero );
                std::cout << x << "\n";
        }

        std::cout << "ZERO = " << Foo::Zero << "\n";

        return 0;
}
Run Code Online (Sandbox Code Playgroud)

dyp*_*dyp 5

#include <string>
#include <iostream>
#include <vector>
#include <type_traits>

namespace detail
{
    template<class T, class = decltype(T::zero)>
    std::true_type has_zero_impl(int);

    template<class T>
    std::false_type has_zero_impl(short);
}

template<class T>
using has_zero = decltype(detail::has_zero_impl<T>(0));

template<class T, class = has_zero<T>>
struct zero
{
    constexpr static T get() { return T(); }
};

template<class T>
struct zero<T, std::true_type>
{
    constexpr static auto get() -> decltype(T::zero)
    {  return T::zero;  }
};
Run Code Online (Sandbox Code Playgroud)

用法示例:

template<>
struct zero<std::string>
{
    static constexpr const char* get()
    {  return "[Empty]";  }
};

struct foo
{
    int m;
    static constexpr int zero = 42;
};

int main()
{
    std::cout << zero<int>::get() << "\n";
    std::cout << zero<std::string>::get() << "\n";
    std::cout << zero<foo>::get() << "\n";
}
Run Code Online (Sandbox Code Playgroud)

紧凑版没有特性:

template<class T>
struct zero
{
private:
    template<class X>
    constexpr static decltype(X::zero) zero_impl(int)
    { return X::zero; }

    template<class X>
    constexpr static X zero_impl(short)
    { return X(); }

public:
    constexpr static auto get()
    -> decltype(zero_impl<T>(0))
    {
        return zero_impl<T>(0);
    }
};

template<>
struct zero<std::string>
{
    constexpr static const char* get()
    {  return "[Empty]";  }
};
Run Code Online (Sandbox Code Playgroud)