C ++标准允许自定义类型的std :: to_string专门化吗? [英] Is specialization of std::to_string for custom types allowed by the C++ standard?

查看:252
本文介绍了C ++标准允许自定义类型的std :: to_string专门化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11和更高版本中,允许在 std 命名空间中专门化 std :: to_string

In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types?

namespace std {
string to_string(::MyClass const & c) { return c.toString(); }
}

示例用例:

int main() {
    MyClass c;
    std::cout << std::to_string(c) << std::endl;
}


推荐答案


在C ++ 11及更高版本中,是否允许在std名称空间中将std :: to_string专门用于自定义类型?

In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types?

否。首先,这不是模板函数,所以您不能

No. First of all, it is not a template function so you can't specialize it at all.

如果您要添加自己的重载函数,答案仍然是不变的。

If you're asking about adding your own overload functions the answer still remains the same.

扩展名称空间std的文档摘要


将声明或定义添加到命名空间std或嵌套在std中的任何命名空间的行为是不确定的,以下有一些例外情况

It is undefined behavior to add declarations or definitions to namespace std or to any namespace nested within std, with a few exceptions noted below

仅当声明依赖于用户定义的类型并且特殊化满足原始模板的所有要求时,才允许将任何标准库模板的模板特殊化添加到命名空间std。

It is allowed to add template specializations for any standard library template to the namespace std only if the declaration depends on a user-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited.






在实践中ce一切可能都可以正常工作,但严格来说,标准说不能保证会发生什么。


In practice everything will probably work just fine but strictly speaking the standard says there is no guarantee of what will happen.

编辑:我无权使用官方标准,因此以下内容来自免费工作草案(N4296)

I don't have access to the official standard so the following is from the free working draft (N4296):


17.6.4.2 使用命名空间

17.6.4.2.1 命名空间标准


  1. 如果C ++程序的声明或定义添加到命名空间std或命名空间std内的
    命名空间中,则C ++程序的行为未定义,除非另有说明。仅当声明依赖于用户定义的类型
    且该专业化满足原始模板的标准库要求且未明确声明时,程序才可以将任何标准库模板的模板专业化
    添加到命名空间std禁止
    181

  2. 如果C ++程序声明以下行为,则其行为是不确定的

  1. The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.181
  2. The behavior of a C++ program is undefined if it declares

2.1-标准库类模板的任何成员函数的显式专业化,或

2.1 — an explicit specialization of any member function of a standard library class template, or

2.2-标准库类的任何成员函数模板的显式专业化类或类模板,

2.2 — an explicit specialization of any member function template of a standard library class or class template, or

2.3-标准库类或类
模板的任何成员类模板的显式或部分专业化。

2.3 — an explicit or partial specialization of any member class template of a standard library class or class template.

仅当声明
依赖于用户定义类型的名称且实例化满足时,程序才可以显式实例化标准库中定义的模板。标准库要求原始模板为

A program may explicitly instantiate a template defined in the standard library only if the declaration depends on the name of a user-defined type and the instantiation meets the standard library requirements for the original template.


这篇关于C ++标准允许自定义类型的std :: to_string专门化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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