Implementing a compile-time “static-if” logic for different string types in a container

后端 未结 5 1331
南笙
南笙 2020-12-14 21:14

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

I\'d like to support both CString

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 22:02

    You could provide function overloads that do what you need:

    size_t getSize(const std::string& str)
    {
        return str.size();
    }
    
    size_t getSize(const CString& str)
    {
        return str.GetLength();
    }
    
    template 
    void DoSomething(const ContainerOfStrings& strings)
    {
        for (const auto & s : strings)
        {
            ...
            auto size = getSize(s);
            ...
        }
    }
    

提交回复
热议问题