SFINAE

Discussion in 'C++' started by kaalus@gmail.com, Apr 26, 2006.

  1. Guest

    Is it possible to use SFINAE to provide different implementations of
    some function depending on the fact that operator << is overloaded for
    some type?

    For example:

    template<class T>
    void output1(const T &t)
    {
    // This implementation should be in effect for types that support
    <<
    std::cout << t;
    }

    template<class T>
    void output2(const T &t)
    {
    // This implementation should be in effect for types that do not
    support <<
    my_output(t);
    }

    So that this usage is possible:

    T t;
    output(t); // uses output1 or output2 depending on << operator
    defined for T
    , Apr 26, 2006
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > Is it possible to use SFINAE to provide different implementations of
    > some function depending on the fact that operator << is overloaded for
    > some type?


    I have no idea what a "SFINAE" is.

    > For example:
    >
    > template<class T>
    > void output1(const T &t)
    > {
    > // This implementation should be in effect for types that support
    > <<
    > std::cout << t;
    > }
    >
    > template<class T>
    > void output2(const T &t)
    > {
    > // This implementation should be in effect for types that do not
    > support <<
    > my_output(t);
    > }
    >
    > So that this usage is possible:
    >
    > T t;
    > output(t); // uses output1 or output2 depending on << operator
    > defined for T


    You could use some kind of traits class.
    Rolf Magnus, Apr 26, 2006
    #2
    1. Advertising

  3. Rolf Magnus wrote:
    > wrote:
    >
    >> Is it possible to use SFINAE to provide different implementations of
    >> some function depending on the fact that operator << is overloaded
    >> for some type?

    >
    > I have no idea what a "SFINAE" is.


    REALLY?!! OK, I'll be the first to tell you. It stands for "Substitution
    Failure Is Not An Error". Look it up on the Web. It's a technique you can
    use to play some interesting tricks using templates.

    > [...]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 26, 2006
    #3
  4. peter koch Guest

    wrote:
    > Is it possible to use SFINAE to provide different implementations of
    > some function depending on the fact that operator << is overloaded for
    > some type?


    It should be: what is your problem?
    >
    > For example:
    >
    > template<class T>
    > void output1(const T &t)
    > {
    > // This implementation should be in effect for types that support
    > <<
    > std::cout << t;
    > }
    >
    > template<class T>
    > void output2(const T &t)
    > {
    > // This implementation should be in effect for types that do not
    > support <<
    > my_output(t);
    > }
    >
    > So that this usage is possible:
    >
    > T t;
    > output(t); // uses output1 or output2 depending on << operator
    > defined for T


    I do not really see the reason to do it with SFINAE (Substitution
    Failure Is Not An Error). The most obvious way to do solve your problem
    would be to provide operator<< for those classes. If that is not
    appropriate, I believe I would simply provide:

    template <typename T>
    void my_output(T const& t) { std::cout << t; }

    /Peter
    peter koch, Apr 26, 2006
    #4
  5. wrote:
    > Is it possible to use SFINAE to provide different implementations of
    > some function depending on the fact that operator << is overloaded for
    > some type?
    >
    > For example:
    >
    > template<class T>
    > void output1(const T &t)
    > {
    > // This implementation should be in effect for types that support
    > <<
    > std::cout << t;
    > }
    >
    > template<class T>
    > void output2(const T &t)
    > {
    > // This implementation should be in effect for types that do not
    > support <<
    > my_output(t);
    > }
    >
    > So that this usage is possible:
    >
    > T t;
    > output(t); // uses output1 or output2 depending on << operator
    > defined for T
    >


    Why not create a container class template something like as follows:

    template <typename T>
    class My_Output
    {
    private:
    const T &val:
    public:
    My_Output(const T &val) : val(inp)
    {
    }
    friend std::eek:stream &operator<<(std::eek:stream &str, const MyOutput
    &inp);
    };

    template <typename T>
    std::eek:stream &operator<<(std::eek:stream &str, const MyOutput<T> &inp)
    {
    // Add your code here

    return str;
    }

    MyClass special;
    // Your code here
    std::cout << My_Output(special) << std::endl;

    Hope this helps.

    JB
    n2xssvv g02gfr12930, Apr 26, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Collingbourne

    problem with SFINAE applied to class methods

    Peter Collingbourne, Jul 1, 2004, in forum: C++
    Replies:
    8
    Views:
    465
    Pete Becker
    Jul 4, 2004
  2. Replies:
    3
    Views:
    1,663
    Attila Feher
    Feb 8, 2005
  3. Clark S. Cox III

    Using SFINAE with constructors

    Clark S. Cox III, Sep 9, 2005, in forum: C++
    Replies:
    2
    Views:
    373
    Howard Hinnant
    Sep 9, 2005
  4. christopher diggins

    SFINAE problem.

    christopher diggins, Sep 26, 2005, in forum: C++
    Replies:
    4
    Views:
    455
    christopher diggins
    Sep 26, 2005
  5. Dilip

    confused with SFINAE

    Dilip, Jul 6, 2006, in forum: C++
    Replies:
    1
    Views:
    330
    Victor Bazarov
    Jul 6, 2006
Loading...

Share This Page