Tuesday Coding Tip 08 — Explicit template specialization

●5 ●27 ●93
calendar_today • schedule1 min read
— Originally published at medium.com

Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.


While template instantiation can be used to move your template implementation from header files to object files for a given subset of types, specialization allows you to override the default template implementation for a particular type.

Specialization requires that you first define the template implementation and then you can implement any number of specializations for given types. This way, you can override free functions (while maintaining their overall signature) or you can override whole classes (and you don’t have to respect their interfaces). This way, you can create drop-in replacements for library containers specialized for particular types and try to optimize them for your use case.

#include <filesystem>
#include <iostream>
#include <string>

template<class T>
T concat(T a, const T& b)
{
    return a + b;
}

namespace fs = std::filesystem;

fs::path concat(fs::path a, const fs::path& b)
{
    return a / b;
}

int main()
{
    using namespace std::string_literals;
    std::cout << concat("aaa"s, "bbb"s) << std::endl;        // outputs: aaabbb
    std::cout << concat("aaa", "bbb").string() << std::endl; // outputs: aaa\bbb
    return 0;
}

The specialization can also be partial and it can be inline/const whatever even if the original template is not.

#include <iostream>
#include <string>

template<class T>
struct CustomType
{
    T foo() { return T {}; }
};

template<>
struct CustomType<std::string>
{
    std::string bar() { return "hello world"; }
};

int main()
{
    CustomType<int> ct1;
    std::cout << ct1.foo() << std::endl; // outputs: 0
    CustomType<std::string> ct2;
    std::cout << ct2.bar() << std::endl; // outputs: "hello world"
}

Last but not least, if you’re implementing a template and need to change only a few lines of implementation for one particular type, you don’t need specialization. Instead, use constexpr conditions — they are evaluated in compile time and optimized away when the compiler emits its own specialized code.

#include <concepts>
#include <filesystem>
#include <iostream>
#include <string>

template<class T>
T concat(T a, const T& b)
{
    if constexpr (std::is_same_v<T, std::filesystem::path>)
        return a / b;
    else
        return a + b;
}

int main()
{
    using namespace std::string_literals;
    using path = std::filesystem::path;
    std::cout << concat("aaa"s, "bbb"s) << std::endl; // outputs: aaabbb
    std::cout << concat(path{ "aaa" }, path{ "bbb" }).string()
              << std::endl; // outputs: aaa\bbb
    return 0;
}

Want more #cpp & #cpp deep dives?

Join 5,976+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 8 of 31 in Tuesday Coding Tips

1 Comment

0 votes

More Posts

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Let's play with C++ reflection

Jakub Neruda - Nov 12

Tuesday Coding Tip 31 — Deducing this

Jakub Neruda - Nov 17

Tuesday Coding Tip 17 — Wrapping C APIs

Jakub Neruda - Jun 30
chevron_left
2.1k Points • 125 Badges
Brno, Czech Republic • linkedin.com/in/jakub-neruda
42Posts
36Comments
22Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.

Related Jobs

Commenters (This Week)

5 comments
3 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!