how to generate code to initialize a std::vector with a custom Zero value if it exists as T::Zero?

kfmfe04

BACKGROUND

I have a container class that has a std::vector<T> member that I initialize with the constructor that takes size_t n_items. I would like to initialize that vector with my own Zero() function which returns 0; by default, but if static member T::Zero exists I want to return that instead.

In my first attempt, I use expression SFINAE, but that failed due to ambiguous overload since both the generic version of Zero and the Zero have the same signature with no arguments. So now, I'm trying to convert the code into classes with operator().

I think I need to use std::enable_if somehow, but I'm not sure how to go about coding this.

FAILED ATTEMPT

#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;
}
dyp
#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;  }
};

Usage example:

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";
}

Compact version w/o trait:

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]";  }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to generate code to initialize a std::vector with a custom Zero value if it exists as T::Zero?

From Dev

Initialize vector to zero

From Dev

How to correctly Initialize the value of an array to zero?

From Dev

How to zero a vector<bool>?

From Dev

efficient way to initialize a vector with zero after constructor

From Dev

How does the compiler initialize local arrays with a default value of zero on the stack?

From Dev

How to initialize a struct of integers to zero?

From Dev

How to initialize to zero/NULL in a template

From Dev

How can zero byte files generate a hash value?

From Dev

Should T() initialize member variables to zero?

From Dev

zero value where there shouldn't be

From Dev

why std::string is zero initialized to indeterminate value

From Dev

why std::string is zero initialized to indeterminate value

From Dev

How to initialize std stack with std vector?

From Dev

Zero and initialize a C structure

From Dev

How to return the value closest to zero

From Dev

MYSQL - how to get zero value

From Dev

Woocommerce custom shortcode returns zero value

From Dev

Can't generate apk: zipalign.exe finished with non-zero exit value 1

From Dev

Zero-initialize data using value-initialized array

From Dev

SQL Select record with MAX non zero value If exists otherwise select record with zero value

From Dev

Find the last non-zero element in a std::vector

From Dev

VBA Code from NULL #VALUE! to Zero

From Dev

VBA Code from NULL #VALUE! to Zero

From Dev

How to initialize std::vector<std::time_t> with n elements in constructor initializer list

From Dev

Why doesn't this templated function to zero-initialize an array compile?

From Dev

Random() returns zero but there is no zero value

From Dev

Why do you initialize Vector2 variables to "Vector2.Zero"?

From Dev

SQL duplication on colA and colB, where colC exists both zero and non-zero value

Related Related

  1. 1

    how to generate code to initialize a std::vector with a custom Zero value if it exists as T::Zero?

  2. 2

    Initialize vector to zero

  3. 3

    How to correctly Initialize the value of an array to zero?

  4. 4

    How to zero a vector<bool>?

  5. 5

    efficient way to initialize a vector with zero after constructor

  6. 6

    How does the compiler initialize local arrays with a default value of zero on the stack?

  7. 7

    How to initialize a struct of integers to zero?

  8. 8

    How to initialize to zero/NULL in a template

  9. 9

    How can zero byte files generate a hash value?

  10. 10

    Should T() initialize member variables to zero?

  11. 11

    zero value where there shouldn't be

  12. 12

    why std::string is zero initialized to indeterminate value

  13. 13

    why std::string is zero initialized to indeterminate value

  14. 14

    How to initialize std stack with std vector?

  15. 15

    Zero and initialize a C structure

  16. 16

    How to return the value closest to zero

  17. 17

    MYSQL - how to get zero value

  18. 18

    Woocommerce custom shortcode returns zero value

  19. 19

    Can't generate apk: zipalign.exe finished with non-zero exit value 1

  20. 20

    Zero-initialize data using value-initialized array

  21. 21

    SQL Select record with MAX non zero value If exists otherwise select record with zero value

  22. 22

    Find the last non-zero element in a std::vector

  23. 23

    VBA Code from NULL #VALUE! to Zero

  24. 24

    VBA Code from NULL #VALUE! to Zero

  25. 25

    How to initialize std::vector<std::time_t> with n elements in constructor initializer list

  26. 26

    Why doesn't this templated function to zero-initialize an array compile?

  27. 27

    Random() returns zero but there is no zero value

  28. 28

    Why do you initialize Vector2 variables to "Vector2.Zero"?

  29. 29

    SQL duplication on colA and colB, where colC exists both zero and non-zero value

HotTag

Archive