是否有办法用模板替换这个预处理器宏,也许使用Boost MPL
Is there is way to replace this preprocessor macro with a template, perhaps using Boost MPL?
我有代码可以创建一个字符或字符串常量,可以是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
相关文章:
- #定义c-预处理器常量..我做错了什么
- 预处理器:插入结构名称中的前一个行号
- 如何在c++中实现处理器调度模拟器
- C/C++预处理器是否可以检测一些编译器选项
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 在clang++预处理器中确定gcc工具链版本
- 不同/较旧的处理器运行c++代码的方式是否不同
- 用于交叉编译和CMake的预处理器宏的单元测试
- 有没有办法在从编译器获取参数时避免预处理器宏?
- 如何比较两个同名的预处理器宏?
- 从预处理器获取 Windows 版本(C++ Win32)
- 如何摆脱为条件编译定义预处理器宏的需要?
- C 预处理器 - 现有定义的预置路径
- VS2015 预处理器定义与点
- 不带预处理器的调用方法/文件的文件名/行号
- 获取文件数据预处理器宏
- 有没有办法在 c++ 中拥有条件预处理器
- 如何在编译时定义C++预处理器指令的值?
- 修复"-Wunused-parameter"取决于预处理器条件的警告
- 是否有办法用模板替换这个预处理器宏,也许使用Boost MPL