草庐IT

C++ 可变参数模板在虚拟抽象的外部未解析

coder 2024-02-22 原文

今天我为我的项目编写代码,并在链接器外部遇到 Unresolved 问题,代码必须生成具有多个虚拟抽象方法的类 - 作为类集合的基础。所以我决定为此任务使用可变参数模板 - 但出现了错误。

template<int I>
struct pin_tag {};

//inputs
template<int N, class T0, class... VAR>
class inputs_base : public inputs_base<N + 1, VAR...>
{
protected:
   typedef inputs_base<N + 1, VAR...> base_type;
   using arg_type = T0;
   //using base_type::_in;
   virtual void _in(T0 const& t, pin_tag<N>) = 0;
};

template<int N, class T0>
class inputs_base<N, T0>
{
protected:
   using arg_type = T0;
   virtual void _in(T0 const& t, pin_tag<N>) = 0;
};

template<class... VAR>
class inputs : public inputs_base<0, VAR...>
{
private:
   using inputs_type = inputs<VAR...>;
   using inputs_base_type = inputs_base<0, VAR...>;

   template <int N, class T = inputs_type>
   struct getter
   {
     using next_type = typename getter<N - 1, typename T::base_type>;
     using arg_type = typename next_type::arg_type;
     using current_type = typename next_type::current_type;
   };

   template <class T>
   struct getter<0, T>
   {
     using arg_type = typename T::arg_type;
     using current_type = typename T;
   };

   public:
   template<int N>
   using in_arg_type = typename getter<N>::arg_type;

   template<int N>
   void in(in_arg_type<N> const& t)
   {
     getter<N>::current_type::_in(t, pin_tag<N>());
   }
};


class test : public inputs< int, bool >
{
protected:
   virtual void _in(int const& val, pin_tag<0>)
   {}
   virtual void _in(bool const& val, pin_tag<1>)
   {}
 };

int _tmain(int argc, _TCHAR* argv[])
{

   test t;
   t.in<0>(100500);
}

我有链接器错误

error LNK2019: unresolved external symbol "protected: virtual void __thiscall inputs_base<0,int,bool>::_in(int const &,struct pin_tag<0>)" (?_in@?$inputs_base@$0A@H_N@@MAEXABHU?$pin_tag@$0A@@@@Z) referenced in function "public: void __thiscall inputs<int,bool>::in<0>(int const &)" (??$in@$0A@@?$inputs@H_N@@QAEXABH@Z)

似乎_in 失去了虚拟性。请任何人检查我的代码。

最佳答案

解决问题

inputs<...>::in() , 替换

getter<N>::current_type::_in(t, pin_tag<N>());

通过

this->_in(t, pin_tag<N>());

另外,在 inputs_base , 还原线

using base_type::_in;

目前已被注释掉。

参见 live example .

_in “失去虚拟性”,因为您在基类 getter<N>::current_type 上显式调用它.相反,this->_in(...)将其称为虚函数。

但是要让它工作,你需要所有 _in范围重载; using做这个。否则,只有 _in最衍生基的重载是可见的。所有其他的都是隐藏的。

清理


您可以使用 std::tuple_element 来简化您的代码而不是(或者,帮助您实现)您的自定义 getter .或者至少删除 getter来自 inputs并将其(或变体)放在一些更通用的“实用程序”位置;您肯定会在其他地方需要它。

此外,

using inputs_type = inputs<VAR...>;

不需要。您可以使用 inputs直接不附加 <VAR...> .这是由编译器在 inputs 范围内为您的方便自动完成的定义。

Here是您清理后的代码。 inputs_base的执行和 inputs代码从 51 行减少到 21 行。

现在inputs_base只做一项工作:声明 _in() 的多个重载.此外,定义 in()作为模板函数,getter 都不是也不std::tuple_element是需要的。特别是,inputs被简化为

template<class... U>
struct inputs : inputs_base<0, U...>
{
    template<int N, typename T>
    void in(T const& t) { this->_in(t, pin_tag<N>()); }
};

泛化


您的代码假定每个虚函数 _in过载是 void并采用单个输入参数,即对 const 的引用.这可能是一个限制。推广这种方法来处理一组任意函数签名将使这段代码非常有用。

Here是行动中的通用代码。时间不多了。现在test修改如下:

struct test : overload<void(int), void(bool)>
// ...

也就是说,它是根据整个函数签名而不是单个参数类型进行参数化的。按值调用适用于 intbool ;不需要 const& .

关于C++ 可变参数模板在虚拟抽象的外部未解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742635/

有关C++ 可变参数模板在虚拟抽象的外部未解析的更多相关文章

  1. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

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

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

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

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

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

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

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

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

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

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

随机推荐