是否有办法用模板替换这个预处理器宏,也许使用Boost MPL

Is there is way to replace this preprocessor macro with a template, perhaps using Boost MPL?

本文关键字:处理器 也许 预处理 Boost MPL 是否 替换      更新时间:2023-10-16

我有代码可以创建一个字符或字符串常量,可以是char类型或wchar_t类型。这使得创建既可以使用字符类型又可以使用字符或字符串常量的模板函数成为可能。到目前为止,我提出的解决方案如下:

template<typename CharType>
CharType CharConstantOfType(const char c, const wchar_t w);
template<>
char CharConstantOfType<char>(const char c, const wchar_t /*w*/)
{
    return c;
}
template<>
wchar_t CharConstantOfType<wchar_t>(const char /*c*/, const wchar_t w)
{
    return w;
}
template<typename CharType>
const CharType* StringConstantOfType(const char* c, const wchar_t* w);
template<>
const char* StringConstantOfType<char>(const char* c, const wchar_t* /*w*/)
{
    return c;
}
template<>
const wchar_t* StringConstantOfType<wchar_t>(const char* /*c*/, const wchar_t* w)
{
    return w;
}

这些模板函数要求你通过提供两个版本的常量来复制该常量。为了解决这些问题,我创建了以下宏:

#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)
#define CHAR_CONSTANT(TYPE, STRING) CharConstantOfType<TYPE>(STRING, TOWSTRING(STRING))
#define STRING_CONSTANT(TYPE, STRING) StringConstantOfType<TYPE>(STRING, TOWSTRING(STRING))

我想消除宏。我想知道是否像Boost MPL库这样的东西可以使这成为可能。

这些宏的一种可能用途是编写一个模板函数来确定字符串是否包含制表符,如下所示。

template<typename CharType>
bool hasTab(const std::basic_string<CharType>& str)
{
    typedef std::basic_string<CharType> string_type;
    const CharType TabChar =  CHAR_CONSTANT(CharType, 't');
    return (str.find(TabChar) != string_type::npos);
}

我想了一下。我不认为有一个有效的转换从wchar_t到char,但另一种方式是合理的。

这里有一种方法,允许您只编写char*版本,并允许程序根据需要创建wchar_t字符串:

#include <iostream>
#include <map>
// introduce the concept of a specialisable converter
template<class FromType, class ToType>
struct converter;
// use the concept
template<class To, class From>
To convert(From f) {
    return converter<From, To>::apply(f);
}
// specialise for non-conversion
template<class T>
struct converter<T, T>
{
    static constexpr T apply(T t) {
        return t;
    }
};
// specialise for converting char to wchar_t
template<>
struct converter<char, wchar_t>
{
    static constexpr wchar_t apply(char t) {
        // do whatever conversion you want here
        return wchar_t(t);
    }
};
// specialise for converting const char* to const wchar_t*
// note - this one is not thread-safe. a mutex will be needed to beef it up in a multi-
// threaded environment
template<>
struct converter<const char*, const wchar_t*>
{
    using string_map_type = std::map<const char*, std::wstring>;
    static string_map_type& string_map() {
        static string_map_type _;
        return _;
    }
    static std::wstring convert_to_wstring(const char*p ) {
        std::wstring result;
        while (*p) {
            result.push_back(convert<wchar_t>(*p++));
        }
        return result;
    }
    static const wchar_t* apply(const char* p) {
        auto& map = string_map();
        auto ifind = map.find(p);
        if (ifind == std::end(map)) {
            ifind = map.emplace(p, convert_to_wstring(p)).first;
        }
        return ifind->second.c_str();
    }
};

using namespace std;
auto main() -> int
{
    cout << "Hello, World" << endl;
    wcout << convert<const wchar_t*>("Hello, World") << endl;
    return 0;
}
预期输出:

Hello, World
Hello, World