草庐IT

c++ - 在嵌套的 lambda 中应用 function_traits 时编译失败

coder 2024-02-18 原文

首先,我有这样的东西,一个重命名的 function_traits 来获取 lambda 的返回类型

template <typename T>
struct FuncAnalyzer
{
};

template <typename T, typename TRet, typename... TArgs>
struct FuncAnalyzer<TRet(T::*)(TArgs...) const>
{
    using TReturn = TRet;
};

template <typename T>
struct FunctionAnalyzer
    : public FuncAnalyzer<decltype(&T::operator())>
{
};

然后当我在一个方法中有这个时,那个 compi:

auto a = [](const int& key) -> QString { return QString::number(key); };
using b = FunctionAnalyzer<decltype(a)>::TReturn;
b x;

但是当我尝试将它放入 lambda 中时,它不起作用

    auto c = [](const int& key) -> QString 
    { 
        auto a = [](const int& key) -> QString { return QString::number(key); };
        using b = FunctionAnalyzer<decltype(a)>::TReturn;
        b x;
        return QString::number(key); 
    };

编译输出:

1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2825: 'T': must be a class or namespace when followed by '::'
1>          Controller\Schema\SchemaController.cpp(105) : see reference to class template instantiation 'ValidSig::FunctionAnalyzer<QString (__cdecl *)(const int &)>' being compiled
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2039: '()' : is not a member of '`global namespace''
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2275: 'T' : illegal use of this type as an expression
1>          Controller\Schema\SchemaController.cpp(105) : see declaration of 'T'
1>          Controller\Schema\SchemaController.cpp(105) : see declaration of 'T'
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2146: syntax error : missing ')' before identifier '()'
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2143: syntax error : missing ',' before ')'
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2947: expecting '>' to terminate template-argument-list, found '>'
1>Controller\Schema\SchemaController.cpp(105): error C2039: 'TReturn' : is not a member of 'ValidSig::FunctionAnalyzer<QString (__cdecl *)(const int &)>'
1>Controller\Schema\SchemaController.cpp(105): error C2061: syntax error : identifier 'TReturn'

我正在使用 MSVC 2013

最佳答案

我们可以将例子简化为

template <class T> struct identity {using type = T;};

template <typename T>
struct FunctionAnalyzer
    : identity<decltype(&T::operator())> {};

int main()
{
    []
    {
        auto a = []{};
        using b = FunctionAnalyzer<decltype(a)>::type;
    }();
}

这编译得很好 with GCC and Clang .
然而,显然 VC++ 错误地传递了 void (__cdecl *)(void) 而不是实际的闭包类型作为模板参数 - 即使静态断言如

    static_assert( std::is_class<decltype(a)>::value, "" );

就在 lambda succeeds 内的行之前.除了说它不正确之外,我真的无法解释这种行为,因为很明显,类类型不是指向函数的指针类型。错误报告应该是适当的。

解决方法是在外部定义 lambda

auto a = []{};
[a] // This is not required by standard! VC++ being stupid I guess
{
    using b = FunctionAnalyzer<decltype(a)>::type;
}();

Demo .

关于c++ - 在嵌套的 lambda 中应用 function_traits 时编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28015297/

有关c++ - 在嵌套的 lambda 中应用 function_traits 时编译失败的更多相关文章

  1. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  2. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  3. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  4. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

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

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

  7. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  8. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  9. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

  10. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

随机推荐