SFINAE and std::numeric_limits

大兔子大兔子 提交于 2019-12-11 15:40:36

问题


I am trying to write a stream class that handles numerical and non-numerical data separately. Can someone explain to me why this code does not compile?

#include <iostream>
#include <cstdlib>

#include <type_traits>
#include <limits>

class Stream
{
public:
    Stream() {};

    template<typename T, typename std::enable_if_t<std::numeric_limits<T>::is_integer::value>>
    Stream& operator<<(const T& val)
    {
        std::cout << "I am an integer type" << std::endl;
        return *this;
    };

    template<typename T, typename std::enable_if_t<!std::numeric_limits<T>::is_integer::value>>
    Stream& operator<<(const T& val)
    {
        std::cout << "I am not an integer type" << std::endl;
        return *this;
    };
};

int main()
{
    Stream s;
    int x = 4;
    s << x;
}

回答1:


Because you are doing SFINAE wrong, and you are also incorrectly using the trait (there is no ::value, is_integer is a boolean). The error with trait is trivial, the problem with SFINAE is that you gave a non-type template parameter to your operator<<, but you never provide an argument for it. You need to specify a default argument.

Sample code:

#include <cstdlib>
#include <iostream>
#include <type_traits>
#include <limits>

class Stream
{
public:
    Stream() {};

    template<typename T, std::enable_if_t<std::numeric_limits<T>::is_integer>* = nullptr>
    Stream& operator<<(const T& val)
    {
        std::cout << "I am an integer type" << std::endl;
        return *this;
    };

    template<typename T, std::enable_if_t<!std::numeric_limits<T>::is_integer>* = nullptr>
    Stream& operator<<(const T& val)
    {
        std::cout << "I am not an integer type" << std::endl;
        return *this;
    };
};

int main()
{
    Stream s;
    int x = 4;
    s << x;
}


来源:https://stackoverflow.com/questions/53124170/sfinae-and-stdnumeric-limits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!