jjzjj

c++ - 带有 C++ 模板的虚假 "use of local variable with automatic storage from containing function"?

coder 2023-11-16 原文

以下代码无法在 g++7.2.0 中编译

template <class Internal>
class Request {
    int content = 0;
public:
    friend void setContent(int i, void *voidptr) {
        Request<Internal> *ptr = (Request<int>*)voidptr;
        ptr->content = i;
    }
    int getContent() {return content;}
};

int main() {
    Request<int> req;
    setContent(4, &req);
    return req.getContent();
}

有错误

test.cpp: In instantiation of ‘void setContent(int, void*)’:
test.cpp:14:23:   required from here
test.cpp:7:14: error: use of local variable with automatic storage from containing function
         ptr->content = i;
         ~~~~~^~~~~~~
test.cpp:6:28: note: ‘Request<Internal>* ptr’ declared here
         Request<Internal> *ptr = (Request<int>*)voidptr;

我不明白这有什么问题(除了愚蠢的例子)。 Clang 4.0.1 似乎接受它,我很确定它是在 g++ 5.4 下编译的 另外:如果我删除所有模板,则编译正常。

这是编译器中的错误还是我违反了一些我不知道的规则?

编辑 它似乎从 gcc 7.x 开始停止工作 https://godbolt.org/g/D6gqcF

最佳答案

我不知道 GCC 是否合理地拒绝这个结构,但似乎完全没有 Request<Internal>setContent类型中正在混淆它。唯一原因可以引用Request<Internal>是注入(inject)的类名,来自定义的范围。

template <class Internal>
class Request {
    int content = 0;
public:
    friend void setContent(int i, void *voidptr); // shouldn't make a difference
    int getContent() {return content;}
};

void setContent(int i, void * voidptr)
{
    // Where does Internal come from?
    Request<Internal> *ptr = (Request<Internal>*)voidptr; 
    ptr->content = i;
}

我不认为你需要这里的模板,因为如果你用第二种类型实例化它,你就违反了单一定义规则。

class RequestBase {
    int content = 0;
public:
    virtual ~RequestBase() = default;
    friend void setContent(int i, RequestBase * ptr) {
        ptr->content = i;
    }
    int getContent() {return content;}
};

如果你类还有其他成员,使用Internal ,您可以使用模板子类添加它们

template <class Internal>
class Request : public RequestBase {
    // members involving Internal
};

另一种选择是使用 InternalsetContent 的参数中

template <class Internal>
class Request {
    Internal content = 0;
public:
    friend void setContent(Internal i, void *voidptr){
        Request<Internal> *ptr = (Request<Internal>*)voidptr;
        ptr->content = i;
    }
    Internal getContent() {return content;}
};

template <class Internal>
class Request {
    int content = 0;
public:
    friend void setContent(int i, Request<Internal> *ptr) {
        ptr->content = i;
    }
    int getContent() {return content;}
};

请注意,您仍然可以绑定(bind) void setContent(int, Request<Internal> *)void (*)(int, void *)函数指针等

关于c++ - 带有 C++ 模板的虚假 "use of local variable with automatic storage from containing function"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48524954/

有关c++ - 带有 C++ 模板的虚假 "use of local variable with automatic storage from containing function"?的更多相关文章

  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 - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

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

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

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

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

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

  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

随机推荐