Home How can I compress these function overloadings using template?
Reply: 0

How can I compress these function overloadings using template?

Edityouprofile
1#
Edityouprofile Published in 2018-01-13 05:54:40Z

As a C++ beginner, I'm trying to write a (maybe a set of) function to join strings that works with delimiter typed char/wchar_t, string/wstring literals and const char/wchar_t*(string literals). Below is my first attempt.

My question is, can I compress these functions into fewer functions using the power of C++ templates? Thanks in advance.

#include <iostream>
#include <locale>
#include <sstream>
#include <string>
#include <vector>

template <typename T = char, typename Arg, typename... Args>
std::basic_string<T> join(
    const std::basic_string<T> &delimiter,
    const Arg &arg,
    const Args &... args) {
    if (!sizeof...(args)) {
        return arg;
    }

    std::basic_ostringstream<T> os;
    os << arg;
    static_cast<void>(
        std::initializer_list<int>{(os << delimiter << args, 0)...});

    return os.str();
}

template <typename Arg, typename... Args>
std::string join(const char *delimiter, const Arg &arg, const Args &... args) {
    return join(std::string(delimiter), arg, args...);
}

template <typename Arg, typename... Args>
std::wstring join(
    const wchar_t *delimiter, const Arg &arg, const Args &... args) {
    return join(std::wstring(delimiter), arg, args...);
}

template <typename Arg, typename... Args>
std::string join(char delimiter, const Arg &arg, const Args &... args) {
    return join(std::string(1, delimiter), arg, args...);
}

template <typename Arg, typename... Args>
std::wstring join(wchar_t delimiter, const Arg &arg, const Args &... args) {
    return join(std::wstring(1, delimiter), arg, args...);
}

int main(int argc, char **argv) {
    std::wcout.imbue(std::locale("zh_CN.UTF-8"));

    std::cout << join(' ', "1", "2", "3") << std::endl;
    std::wcout << join(L' ', L"一", L"二", L"三") << std::endl;
    std::cout << join(std::string("--"), "1", "2", "3") << std::endl;
    std::cout << join("--", "1", "2", "3") << std::endl;
    std::wcout << join(std::wstring(L"--"), L"一", L"二", L"三") << std::endl;
    std::wcout << join(L"--", L"一", L"二", L"三") << std::endl;

    return 0;
}

Updated:

template <typename T = char, typename Arg, typename... Args>
std::basic_string<T> join(
    const std::basic_string<T> &delimiter,
    const Arg &arg,
    const Args &... args) {
    if (!sizeof...(args)) {
        return arg;
    }

    std::basic_ostringstream<T> os;
    os << arg;
    static_cast<void>(
        std::initializer_list<int>{(os << delimiter << args, 0)...});

    return os.str();
}

template <typename T = char, typename Arg, typename... Args>
std::basic_string<T> join(
    const T *delimiter, const Arg &arg, const Args &... args) {
    return join(std::basic_string<T>(delimiter), arg, args...);
}

template <typename T = char, typename Arg, typename... Args>
std::basic_string<T> join(T delimiter, const Arg &arg, const Args &... args) {
    return join(std::basic_string<T>(1, delimiter), arg, args...);
}

Compress into three..., but I wonder whether it can be compressed into two(take delimiter as a string or a char).

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330597 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO