Thread: Multiple template specializations with the same body

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    Multiple template specializations with the same body

    Is it possible to do template specializations with the same body for unrelated types?

    Code:
    template <typename T>
    void f(T &x) { ... default implementation }
    
    template<>
    void f(A &x) { ... specialized implementation for T=A }
    
    template<>
    void f(B &x) { ... specialized implementation for T=B }
    If the bodies of the 2 specializations are the same (even though A and B are totally unrelated types), is there a way to remove code duplication?

  2. #2
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    you can make some behavioral-implementation for group of types denoted for example by "T1" and play with std::enable_if to make this implementation for everything that is T1 (or not T).
    If you give us some actual case, we'll be able to give an actual example.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah! That makes sense.

    I think I can work it out from type traits and enable_if. Just needed the hint.

    Thanks!

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I think I can work it out from type traits and enable_if. Just needed the hint.
    O_o

    You probably didn't really want to specialize function templates in any event.

    The moral of the code is that specializations don't overload functions, qualifiers participate in both levels of resolution, and specialized function templates are templates to the compiler but not the linker.

    Soma

    Code:
    #include <iostream>
    
    struct SA{SA(){}};
    
    struct SB{SB(){}};
    
    template
    <
        typename F
    >
    void Dump1
    (
        F & f
    )
    {
        std::cout << "g" << '\n';
    }
    
    template <> void Dump1
    (
        SA & f
    )
    {
        std::cout << "a" << '\n';
    }
    
    template <> void Dump1
    (
        SB & f
    )
    {
        std::cout << "b" << '\n';
    }
    
    template
    <
        typename F
    >
    void Dump2
    (
        F f
    )
    {
        std::cout << "g" << '\n';
    }
    
    template
    <
        typename F
    >
    void Dump2
    (
        F * f
    )
    {
        std::cout << "g *" << '\n';
    }
    
    template <> void Dump2
    (
        SA * f
    )
    {
        std::cout << "a *" << '\n';
    }
    
    template <> void Dump2
    (
        SB * f
    )
    {
        std::cout << "b *" << '\n';
    }
    
    template
    <
        typename F
    >
    void Dump3
    (
        F f
    )
    {
        std::cout << "g" << '\n';
    }
    
    template <> void Dump3
    (
        SA * f
    )
    {
        std::cout << "a *" << '\n';
    }
    
    template <> void Dump3
    (
        SB * f
    )
    {
        std::cout << "b *" << '\n';
    }
    
    template
    <
        typename F
    >
    void Dump3
    (
        F * f
    )
    {
        std::cout << "g *" << '\n';
    }
    
    int main()
    {
        SA sA1;
        const SA sA2;
        SB sB1;
        const SB sB2;
        Dump1(sA1);
        Dump1(sA2);
        Dump1(sB1);
        Dump1(sB2);
        Dump2(&sA1);
        Dump2(&sA2);
        Dump2(&sB1);
        Dump2(&sB2);
        Dump3(&sA1);
        Dump3(&sA2);
        Dump3(&sB1);
        Dump3(&sB2);
        return(0);
    }
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple template specialized types
    By petey in forum C++ Programming
    Replies: 8
    Last Post: 02-23-2012, 01:15 PM
  2. Multiple inheritance from same base template ambiguity
    By TotalTurd in forum C++ Programming
    Replies: 6
    Last Post: 12-09-2010, 03:40 AM
  3. Double template specializations
    By Sang-drax in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2006, 01:25 PM
  4. differences between Partial Explicit Specializations
    By sawer in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2006, 04:29 AM
  5. Any body know....
    By swgh in forum Game Programming
    Replies: 1
    Last Post: 01-25-2006, 05:41 AM