草庐IT

c++ - 非类型模板参数可以是 "void*"类型吗?

coder 2023-11-13 原文

Yakk - Adam Nevraumont said :

Non-type template parameters of type void* are not allowed in at least some versions of the standard.

这是真的吗? 如果为真,在哪些版本的标准中不允许类型为 void* 的非类型模板参数?

(注意:如 a comment 回答 another comment , 这是关于非类型模板参数, 不是模板类型参数, 可以是任何有效的 type-id [temp.arg.type] , 包括 void*

最佳答案

长话短说

void* 类型的模板参数自 C++20 起有效。 它们在 C++20 之前无效。

C++20

C++20 放宽了对非类型模板参数类型的限制, 所以让我们先调查一下。

当前草案(截至 2019 年 5 月 6 日 UTC 10:00)在 [temp.param]/4 中表示:

A non-type template-parameter shall have one of the following (optionally cv-qualified) types:

  • a literal type that has strong structural equality ([class.compare.default]),
  • an lvalue reference type,
  • a type that contains a placeholder type ([dcl.spec.auto]), or
  • a placeholder for a deduced class type ([dcl.type.class.deduct]).

void*是指针类型。 指针类型是标量类型 ( [basic.types]/9 )。 标量类型文字类型 ( [basic.types]/10 )。 因此,void*是一个文字类型。 第一个项目符号是相关的。

进一步追踪,[class.compare.default]/3说:

A type C has strong structural equality if, given a glvalue x of type const C, either:

  • C is a non-class type and x <=> x is a valid expression of type std::strong_ordering or std::strong_equality, or

  • C is a class type with an == operator defined as defaulted in the definition of C, x == x is well-formed when contextually converted to bool, all of C's base class subobjects and non-static data members have strong structural equality, and C has no mutable or volatile subobjects.

void*是非类类型, 所以第一个项目符号是相关的。 现在问题归结为 x <=> x 的类型 其中 xvoid* const 类型的左值(不是 const void* )。 每[expr.spaceship]/8 :

If the composite pointer type is an object pointer type, p <=> q is of type std::strong_­ordering. If two pointer operands p and q compare equal ([expr.eq]), p <=> q yields std::strong_­ordering::equal; if p and q compare unequal, p <=> q yields std::strong_­ordering::less if q compares greater than p and std::strong_­ordering::greater if p compares greater than q ([expr.rel]). Otherwise, the result is unspecified.

请注意 void*是一个对象指针类型 ( [basic.compound]/3 )。 因此,x <=> x类型为 std::strong_ordering . 因此类型 void*具有强结构平等

因此,在当前的 C++20 草案中, void*允许作为模板参数类型的类型。

C++17

现在我们讨论 C++17。 [temp.param]说:

A non-type template-parameter shall have one of the following (optionally cv-qualified) types:

  • integral or enumeration type,
  • pointer to object or pointer to function,
  • lvalue reference to object or lvalue reference to function,
  • pointer to member,
  • std​::​nullptr_­t, or
  • a type that contains a placeholder type.

请注意,“指向对象的指针”不包括 void*[basic.compound]/3 :

[ Note: A pointer to void does not have a pointer-to-object type, however, because void is not an object type. — end note ]

以上六个项目符号都不包括void* 作为模板参数的可能类型。 因此,在 C++17 中, 模板参数不应具有类型 void* .

C++11和C++14的写法是一样的 除了关于占位符类型的项目符号不存在。 一般来说, 在 C++20 之前, 模板参数不应具有类型 void* .

但是编译器会诊断这个吗?

T.C.在一个说 comment 没有人诊断这个 IHRC。 让我们测试编译器是否在 C++17 模式下诊断 使用下面显示的最小示例:

template <void*>
class C {};

int main()
{
    C<nullptr> x;
    (void) x;
}

代码编译并运行良好 GCC 9.1.0 , GCC 8.3.0 , GCC 7.3.0 , GCC 6.3.0 , GCC 5.5.0 , Clang 8.0.0 , Clang 7.0.0 , Clang 6.0.1 , 和 Clang 5.0.0 .

NathanOlivercomment中告诉我有人告诉他有些编译器会出错,但主要的不会。因此,就我在这里能够确认的而言,T.C. 的说法是正确的——没有人对此做出诊断。

关于c++ - 非类型模板参数可以是 "void*"类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56003162/

有关c++ - 非类型模板参数可以是 "void*"类型吗?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  5. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  6. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  7. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  8. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  9. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  10. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

随机推荐