且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在现代C ++中,是否可以将字符串文字作为参数传递给C ++模板?

更新时间:2022-11-12 15:46:29

是,在

问题是确定模板非类型参数的唯一性很困难。

The problem was that determining uniqueness of a template non-type argument was difficult.

< => 宇宙飞船的操作员比较。如果它是非用户提供的(并且仅基于非用户提供的< => ,则递归重复)(和其他一些要求;请参见p0732 ),该类型可以用作非类型模板参数。

c++20 adds in a <=> spaceship operator comparison. If it is non-user provided (and based only off non-user provided <=> in turn, repeat recursively) (and a few other requirements; see p0732), the type can be used as a non-type template argument.

可以从 constexpr 构造函数中的原始字符串 构造此类类型,包括使用演绎指南它们会自动调整大小。

Such types can be constructed from raw "strings" in constexpr constructors, including using c++17 deduction guides to make them auto-size themselves.

由于存储的数据大小可能是该类型的一部分,因此您需要将该类型作为 auto 键入的非类型参数或自动推断的类型。

As the size of the data stored is probably going to be part of the type, you'll want to take the type as an auto typed non-type parameter or otherwise auto-deduced type.

请注意,将模板的实现放在cpp文件中通常是个坏主意。但这是另一个问题。

Note that placing the implementation of your template in a cpp file is usually a bad idea. But that is another question.