jjzjj

c++ - STL 中的 VS 编译器错误 C2752 ("more than one partial specialization matches")

coder 2024-02-02 原文

不知何故,我喜欢这些显示(基本?)问题的“最短”程序。在 VS2008 中测试一些模板代码时出现了这个错误(它也已在 VS2010 和 VS2012 中得到确认,见下文):

c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(225) : error C2752: 'std::_Ptr_cat_helper<_T1,_T2>' : more than one partial specialization matches the template argument list

   with
   [
       _T1=const float (**),
       _T2=const float (**)
   ]

我可以将问题归结为以下三行:

#include <vector>
typedef float TPoint[3];
std::vector<TPoint const*> points; // error C2752

注意以下都ok

#include <vector>
#include <list>
typedef float TPoint[3];
// these similar usages of TPoint are all ok:
std::vector<TPoint*> points; // no error
TPoint const* points1[2];
std::list<TPoint const*> points2;

我试图通过为 struct _Ptr_cat_helper 提供额外的模板特化来修复 xutility - 但不幸的是。任何想法出了什么问题?或者如何在不丢失 const 的情况下变通?

最佳答案

问题确实是部分特化中的歧义:

在内部,分配器使用一些元编程 (_Ptr_cat) 来确定是否要对 vector 的元素调用 dtor(或什么都不做)。这种元编程试图通过使用部分特化来区分某些情况。 _Ptr_cat使用 _Ptr_cat_helper它专用于 vector 的分配器指针类型—— vector 的 value_typeTPointer const* == const float (*)[3] , 所以 vector 的分配器有一个指针类型 const float(**)[3] .

当您使用 std::vector < const float(*)[3] > 时,错误消息包含 [3]部分,而使用 std::vector < TPoint const* > , [3]不显示o.O

_Ptr_cat期望具有可能不同的 c 限定符的相同类型的两个模板参数,例如float*, float const* . 现在,这两种输入类型都是 const float (**)[3] . MSVC 将它们模糊地解析为 _Ptr_cat_helper 的特化。 : Ty**, Ty const**和/或 Ty**, Ty** .我通过编写一个模仿 _Ptr_cat_helper 的部分特化的小例子来验证这一点(只是普通模板,不涉及标准库)。

然而,我无法解释为什么会这样。奇怪的是,在仅使用一个特化参数 -- const float(**)[3] 设置示例时没有歧义。解析为 Ty const**Ty = float const[3]正如预期的那样。

感谢 Peter Alexander,我还使用 g++ 尝试了我的简单示例(2 个模板参数),它按预期工作,没有歧义。也许这可能是编译器问题?

让我提出一些解决方法:

  • 如果你真的想修改 MSVC 标准库,你可以添加一个特化 _Ptr_cat_helper < const Ty (**)[3], const Ty (**)[3] >这完全符合并解决了歧义。遗憾的是,您必须明确提供维度 (3)。
  • 此处不要使用数组。使用 std::array (在 VS08 中的 tr1 中可用)或结构或普通指针( float const* 而不是 float const[3] )
  • 使用一个简单的包装器:

    template < typename T > struct wrapper { T wrapped; }
    std::vector < wrapper < TPoint > const* > m;
    

    对我来说很好。

编辑:这是我使用的示例:

#include <typeinfo>
#include <iostream>

template < typename T1,  typename T2 >
struct Spec
{
    static const char* check() { return "plain"; }
    typedef void Value;
};

#define MAKE_SPEC(ARG0, ARG1) \
template < typename T > \
struct Spec < ARG0, ARG1 > \
{ \
    static const char* check() { return #ARG0 ", " #ARG1; } \
    typedef T Value; \
}

MAKE_SPEC(T**, T**);
MAKE_SPEC(T**, T const**);
// can do more, but need not to..

int main()
{
    typedef Spec < const float(**)[3], const float(**)[3] > MySpec;

    std::cout << MySpec::check() << " -- " << typeid(MySpec :: Value).name();
}

关于c++ - STL 中的 VS 编译器错误 C2752 ("more than one partial specialization matches"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12762781/

有关c++ - STL 中的 VS 编译器错误 C2752 ("more than one partial specialization matches")的更多相关文章

  1. 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

  2. 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""-

  3. ruby-on-rails - Railstutorial : db:populate vs. 工厂女孩 - 2

    在railstutorial中,作者为什么选择使用这个(代码list10.25):http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-usersnamespace:dbdodesc"Filldatabasewithsampledata"task:populate=>:environmentdoRake::Task['db:reset'].invokeUser.create!(:name=>"ExampleUser",:email=>"example@railstutorial.org",:passwo

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

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

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

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

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  8. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. ruby - RVM "ERROR: Unable to checkout branch ."单用户 - 2

    我在新的Debian6VirtualBoxVM上安装RVM时遇到问题。我已经安装了所有需要的包并使用下载了安装脚本(curl-shttps://rvm.beginrescueend.com/install/rvm)>rvm,但以单个用户身份运行时bashrvm我收到以下错误消息:ERROR:Unabletocheckoutbranch.安装在这里停止,并且(据我所知)没有安装RVM的任何文件。如果我以root身份运行脚本(对于多用户安装),我会收到另一条消息:Successfullycheckedoutbranch''安装程序继续并指示成功,但未添加.rvm目录,甚至在修改我的.bas

随机推荐