Pietro
Pietro

Reputation: 13220

Template specialisation: member function with parameter different from T

Is it possible to implement something like what follows using template specialisation?

#include <iostream>

template< typename T, bool a, bool b>
class Test
{
    T value;

public:
    Test() {}
    Test( const T& val ) : value( val ) {}

    void Set( const T& val ) { // [1]
        value = val;
    }

    void Set( const float val ) { // [2] To run just for T != float
        ...
    }
};

int main()
{
    Test<int, true, true> test1;
    test1.Set( 1234 ); // Run [1]

    Test<float, true, true> test2;
    test2.Set( 1.234f ); // Run [1]

    Test<int, true, true> test3;
    test3.Set( 1.234f ); // Run [2]
}

Is there a syntax to specify that a member function is the one to be selected when T is different from float?

Upvotes: 1

Views: 87

Answers (3)

Max Langhof
Max Langhof

Reputation: 23711

A clean option would be to use (non-polymorphic) inheritance to do the specialization without repeating everything:

namespace detail
{
    template< typename T, bool a, bool b>
    class TestImplBase
    {
    protected:
        T value;

    public:
        TestImplBase() {}
        TestImplBase( const T& val ) : value( val ) {}

        void Set( const T& val ) { // [1]
            value = val;
        }
    };

    // General case
    template< typename T, bool a, bool b>
    class TestImpl : public TestImplBase<T, a, b>
    {
    public:
        using TestImplBase<T, a, b>::TestImplBase; // keep the same constructors
        using TestImplBase<T, a, b>::Set; // See comment by Jarod42

        // Has [1] as well.

        void Set( const float val ) { // [2] To run just for T != float
            //...
        }
    };

    // Specialization for T == float
    template<bool a, bool b>
    class TestImpl<float, a, b> : public TestImplBase<float, a, b>
    {
    public:
        using TestImplBase<float, a, b>::TestImplBase; // keep the same constructors

        // Only has [1], not [2].
    };
}

template< typename T, bool a, bool b>
using Test = detail::TestImpl<T, a, b>;

https://godbolt.org/z/zRieA2

This is a very general approach, possibly a bit too general for your case, but that's hard to say from this small piece of code.

Upvotes: 1

Jarod42
Jarod42

Reputation: 218238

With C++20, it would be very simple:

requires allow to "discard" method:

template <typename T, bool a, bool b>
class Test
{
    T value;
public:
    Test() {}
    Test(const T& val) : value( val ) {}

    void Set(const T& val) { value = val; }

    void Set(float val) requires (!std::is_same<T, float>::value) {
        // ...
    }
};

Else SFINAE is generally the way to remove the overload (but requires template function, so you have to make one of your method template).

Upvotes: 1

max66
max66

Reputation: 66230

Is it possible to implement something like what follows using template specialisation?

Yes: specializing the full class Test: generic case, with "[2]", and float case, without it.

Otherwise I suppose you can use SFINAE to disable "[2]" when T is float

template <typename U = T>
std::enable_if_t<false == std::is_same_v<U, float>> Set( const float val )
 { /* ... */ }

Or maybe also

template <typename U = T>
std::enable_if_t<false == std::is_same_v<U, float> && true == std::is_same_v<U, T>>
      Set( const float val )
 { /* ... */ }

if you want to avoid that Set() is enabled, in float case, explicating the U template type.

Upvotes: 1

Related Questions