首页 > 解决方案 > 用户定义文字的别名,即'使用 NS::operator ""_suffix' 和另一个名称

问题描述

考虑一些库定义:

namespace NS {
    constexpr atype operator ""_suffix(const char*, std::size_t);
};

如果我不喜欢这个名称_suffix(可能与其他库冲突,或者与我的命名约定形成鲜明对比):我可以“重命名”操作员吗?

using _myalias = NS::operator ""_suffix`;  // syntax error
using _myalias = NS::""_suffix`;  // syntax error
using _myalias = NS::_suffix`;  // undefined (`operator ""` is part of its name)

constexpr decltype(auto) operator ""_myalias(const char *str, std::size_t len)
{
    return NS::operator ""_suffix(str, len);  // very verbose, but it works
}

那么,是否有一种简单的语法来为用户定义的文字定义别名?

标签: c++user-defined-literalsusing-declaration

解决方案


推荐阅读