草庐IT

c++ - 尾随返回类型和标签分发

coder 2024-02-26 原文

试验尾随返回类型和标记分派(dispatch),我编写了以下代码。

#include <string>
#include <iostream>

using namespace std;

namespace Params
{
struct t_param1{};
struct t_param2{};
};

template<typename t_detail>
struct Select;

template<>
struct Select<Params::t_param1> {using choice = Params::t_param1;};

template<>
struct Select<Params::t_param2> {using choice = Params::t_param2;};

class Tester
{
private:
    using t_uint32 = uint32_t;
    using t_string = string;

private:
    t_uint32 m_param1;
//      t_string m_param2;

private:
    template<typename t_entity>
    void assign(const Params::t_param1&, t_entity&& entity);

    template<typename t_entity>
    void assign(const Params::t_param2&, t_entity&& entity);

    auto access(const Params::t_param1&) -> decltype(m_param1);
//      auto access(const Params::t_param2&) -> decltype(m_param2);


public:
    template<typename t_detail, typename t_entity>
    void assign(t_entity&& entity);

    template<typename t_detail>
    auto access() -> decltype(access(typename Select<t_detail>::choice()));
};

template<typename t_detail, typename t_entity>
void
Tester::assign(t_entity&& entity)
{
assign(typename Select<t_detail>::choice(), entity);
}


template<typename t_entity>
void
Tester::assign(const Params::t_param1&, t_entity&& entity)
{
m_param1 = entity;
cout << "Assigned m_param1 with " << entity << endl;
}

/*
template<typename t_entity>
void
Tester::assign(const Params::t_param2&, t_entity&& entity)
{
m_param2 = entity;
cout << "Assigned m_param2 with " << entity << endl;
}
*/



template<typename t_detail>
auto
Tester::access()
-> decltype(access(typename Select<t_detail>::choice()))
{
return(access(typename Select<t_detail>::choice()));
}

auto
Tester::access(const Params::t_param1&)
-> decltype(m_param1)
{
return(m_param1);
}

/*
auto
Tester::access(const Params::t_param2&)
-> decltype(m_param2)
{
return(m_param2);
}
*/

int main() {
auto tester = Tester();
tester.assign<Params::t_param1>(79);
//  tester.assign<Params::t_param2>("viziv");

auto param1 = tester.access<Params::t_param1>();
//  auto param2 = tester.access<Params::t_param2>();

cout << "Access: param1 = " << param1 << endl;
//  cout << "Access: param2 = " << param2 << endl;

return 0;
}

当我使用 Apple LLVM 版本 7.0.2 (clang-700.1.81) 编译此代码时,出现以下编译错误

junk1.cpp:78:9: error: out-of-line definition of 'access' does not match any declaration in 'Tester'
Tester::access()
        ^~~~~~
1 error generated.

奇怪的是,当我取消注释分配和访问 param2 的代码(在上面的代码中被注释掉)时,它可以正常编译并产生所需的结果。

我做错了什么?谁能向我解释为什么包含 param2 会改变编译行为?

最佳答案

我认为这里存在一个半问题。

首先在于使用尾随返回类型实质上创建了一个模板化函数。当您尝试使用类的功能时,类的类型不能不完整。

这就是为公众移动函数定义的原因access类声明中的方法修复它(Demo);只要公众access,该类(class)就不完整方法尚未定义,并且在类完成之前无法定义该方法。

请注意,另一种解决此问题的方法是将 access 的私有(private)版本在某种程度上是非成员函数(例如,周围范围内的自由 float 函数)。

这种方法的问题(问题的一半,因为你实际上并没有尝试这样做)是,试图调用现在免费的 float 版本 access要求编译器评估所有可能的重载,包括公共(public)模板化 access (感谢ADL)。发生这种情况时,Select<t_detail>::choicenon-deduced context 中评估, 无法获取到实际的底层类型。

所以,如果我们都移动私有(private) accessTester之外 将其重命名(类似于 access2 ),然后我们可以将公开的声明和定义分开 access功能(Demo)

关于c++ - 尾随返回类型和标签分发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35484770/

有关c++ - 尾随返回类型和标签分发的更多相关文章

  1. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  2. ruby - 在院子里用@param 标签警告 - 2

    我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?

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

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

  4. ruby - Infinity 和 NaN 的类型是什么? - 2

    我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串

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

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

  6. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  7. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  8. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  9. ruby-on-rails - 在 Rails 开发环境中为 .ogv 文件设置 Mime 类型 - 2

    我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain

  10. ruby - Ruby 中的隐式返回值是怎么回事? - 2

    所以我开始关注ruby​​,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出

随机推荐