实现编译时的“ static-if”。容器中不同字符串类型的逻辑 [英] Implementing a compile-time "static-if" logic for different string types in a container

查看:145
本文介绍了实现编译时的“ static-if”。容器中不同字符串类型的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个在字符串容器上运行的函数模板,例如 std :: vector

I'd like to write a function template that operates on a container of strings, for example a std::vector.

我想使用相同的模板功能同时支持 CString std :: wstring

I'd like to support both CString and std::wstring with the same template function.

问题是 CString 和wstring具有不同的接口,例如,获取a的长度 CString ,则调用 GetLength()方法,而对于wstring,则调用 size() length()

The problem is that CString and wstring have different interfaces, for example to get the "length" of a CString, you call the GetLength() method, instead for wstring you call size() or length().

如果我们有静态 功能在C ++中,我可以这样写:

If we had a "static if" feature in C++, I could write something like:

template <typename ContainerOfStrings>
void DoSomething(const ContainerOfStrings& strings)
{
    for (const auto & s : strings)
    {
        static_if(strings::value_type is CString) 
        {
            // Use the CString interface
        }
        static_else_if(strings::value_type is wstring)
        {   
            // Use the wstring interface
        }
    }
}

是否有一些模板编程技术可以实现这一目标使用当前可用的C ++ 11/14工具的目标是什么?

Is there some template programming technique to achieve this goal with currently available C++11/14 tools?

PS

我知道可以写一些的 DoSomething()重载带有 vector< CString> vector< wstring> ,但这不是问题的重点。

而且,我希望此函数模板适用于您可以使用range-for循环进行迭代的任何容器。

PS
I know it's possible to write a couple of DoSomething() overloads with vector<CString> and vector<wstring>, but that's not the point of the question.
Moreover, I'd like this function template to work for any container on which you can iterate using a range-for loop.

推荐答案

#include <type_traits>

template <typename T, typename F>
auto static_if(std::true_type, T t, F f) { return t; }

template <typename T, typename F>
auto static_if(std::false_type, T t, F f) { return f; }

template <bool B, typename T, typename F>
auto static_if(T t, F f) { return static_if(std::integral_constant<bool, B>{}, t, f); }

template <bool B, typename T>
auto static_if(T t) { return static_if(std::integral_constant<bool, B>{}, t, [](auto&&...){}); }

测试:

template <typename ContainerOfStrings>
void DoSomething(const ContainerOfStrings& strings)
{
    for (const auto & s : strings)
    {
        static_if<std::is_same<typename ContainerOfStrings::value_type, CString>{}>
        ([&](auto& ss)
        {
            // Use the CString interface
            ss.GetLength();
        })(s);

        static_if<std::is_same<typename ContainerOfStrings::value_type, wstring>{}>
        ([&](auto& ss)
        {
            // Use the wstring interface
            ss.size();
        })(s);
    }
}

演示

这篇关于实现编译时的“ static-if”。容器中不同字符串类型的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆