草庐IT

c++ - 在可变参数模板中扩展了错误的包

coder 2024-02-05 原文

可变参数模板出现了一个非常奇怪的问题。似乎扩展了错误的包。这是一个代码片段:

#include <tuple>

template<typename...>
struct types {};

template<typename = types<>>
struct Base;

template<typename... Args1>
struct Base<types<Args1...>> {
    template<typename... Args2>
    static auto construct(Args1... args1, Args2&&... args2)
        -> decltype(std::make_tuple(args1.forward()..., std::declval<Args2>()...))
    {
        return std::make_tuple(args1.forward()..., std::forward<Args2>(args2)...);
    }
};

struct Derived : Base<> {};

int main() {
    auto test = &Derived::construct<char const(&)[7]>;
}

我收到这个错误:

13 : <source>:13:43: error: request for member 'forward' in 'args2#0', which is of non-class type 'const char [7]'
         -> decltype(std::make_tuple(args1.forward()..., std::declval<Args2>()...))
                                     ~~~~~~^~~~~~~
13 : <source>:13:43: error: request for member 'forward' in 'args2#0', which is of non-class type 'const char [7]'
<source>: In function 'int main()':
22 : <source>:22:27: error: unable to deduce 'auto' from '& construct<const char (&)[7]>'
     auto test = &Derived::construct<char const(&)[7]>;
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
22 : <source>:22:27: note:   could not resolve address from overloaded function '& construct<const char (&)[7]>'
Compiler exited with result code 1

但是,当包中有值时,它不会发生:

struct HasForward { int forward() { return 0; } };

struct Derived : Base<types<HasForward>> {};

这是 First snippet live Second snippet live

这段代码有什么问题?这是编译器错误吗?有什么方法可以克服它并让第一个包空着吗?

最佳答案

Is this a compiler bug? Is there any ways to overcome it and leave the first pack empty?

它看起来像是你的编译器中的一个错误。
要解决此问题,您可以使用如下例中的函数声明(无需定义)并使用它来测试您的参数:

template<typename... Args1>
class Base<types<Args1...>> {
    template<typename... T, typename... U>
    static auto ret(types<T...>, types<U...>)
    -> decltype(std::make_tuple(std::declval<T>().forward()..., std::declval<U>()...));

public:
    template<typename... Args2>
    static auto construct(Args1... args1, Args2&&... args2)
    -> decltype(ret(types<Args1...>{}, types<Args2...>{}))
    {
        return std::make_tuple(args1.forward()..., std::forward<Args2>(args2)...);
    }
};

有点丑,但是it works当您的第一个包为空时(也按要求在 C++11 中)并且链接器应丢弃所有内容。

--- 编辑

正如@W.F. 所建议的。在评论中(感谢您的建议,我没有注意到),它更容易完成。
只需按如下方式定义您的函数:

static auto construct(Args1... args1, Args2&&... args2)
    -> decltype(std::make_tuple(std::declval<Args1>().forward()..., std::declval<Args2>()...))
{
    return std::make_tuple(args1.forward()..., std::forward<Args2>(args2)...);
}

关于c++ - 在可变参数模板中扩展了错误的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47114738/

有关c++ - 在可变参数模板中扩展了错误的包的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  3. 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您的程序将作为解释器的子进程执行。除

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

  7. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  8. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  9. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

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

随机推荐