jjzjj

decltype

全部标签

c++ - 使用 decltype() 声明函数签名

是否可以将函数bar声明为与函数foo具有相同的签名?intfoo(inta){return0;}decltype(foo)bar{return1;}//imaginarysyntax 最佳答案 我认为这同样适用于typedef和别名:您可以使用decltype来声明一个函数,但不能定义它:intfoo();decltype(foo)bar;intfoo(){returnbar();}intbar(){return0;}被clang++3.5和g++4.8.1接受[dcl.fct.def.general]/2禁止(语法上)不带括号的

c++ - 通过 decltype 表达式调用时 static_assert 是否应该工作?

我预计以下代码会因最后一行的static_assert检查而失败。但是在MSVC2015和gcc6.2中,它编译成功。它确实无法按预期在clang3.9中进行编译。这是编译器错误还是static_assert在decltype()中不起作用?#include#includetemplatestructWrapper{};templateconstexprstd::tupleoperator|(Wrapper,Wrapper){static_assert(std::is_same::value==false,"can'tcombinetwoofthesametype");returnst

c++ - 为什么 decltype(declval<T>().func()) 在 decltype(&T::func) 不工作的地方工作?

我试图检测成员函数baz()的存在在模板参数中:templatestructImplementsBaz:publicstd::false_type{};templatestructImplementsBaz:publicstd::true_type{};但它总是产生错误:structFoo{};structBar{voidbaz(){}};std::cout::value::value使用declval不过,调用该方法确实有效:templatestructImplementsBaz().baz())>:publicstd::true_type{};当然,现在这个只能检测一个baz具有0

c++ - 类方法声明中的 decltype : error when used before "referenced" member is declared

考虑followingcode:structtest{autofunc()->decltype(data){}//ERRORintdata;};intmain(){testt;t.func();}它给出了以下错误:main.cpp:2:29:error:'data'wasnotdeclaredinthisscopeautofunc()->decltype(data){}但是,如果我将data放在func()之上,它不会给出任何错误(livecode):structtest{intdata;autofunc()->decltype(data){}};...所以我的问题是,为什么declt

c++ - 重载成员函数的 decltype

这个问题在这里已经有了答案:Disambiguateoverloadedmemberfunctionpointerbeingpassedastemplateparameter(1个回答)关闭6年前。我有这个代码:structFoo{intprint(inta,doubleb);intprint(inta);voidprint();voidprint(inta,intb,intc);voidother();};我可以打电话decltype(&Foo::other)但是打电话decltype(&Foo::print)以错误结束,这对我来说很清楚。但是我怎样才能更“精确地”指定四种print

c++ - C++14 中 decltype(auto) 的转换函数

classA{public:intnum;A(intparam):num(param){}operatordecltype(auto)(){returnnum;}};classB{public:intnum;AobjA;B(intparam):num(param),objA(param){}//operatorA(){returnobjA;}//Works//#1//operatorint(){returnobjA;}//Works//#2//operatorchar(){returnobjA;}//ActuallyNotNeeded//#3//operatordouble(){ret

c++ - C++17 中结构化绑定(bind)引入的标识符有哪些类型?

据我所知,C++17中结构化绑定(bind)引入的标识符实际上是对某些“隐藏”变量的引用。这样auto[a,b]=std::make_tuple(1,2);有点等同于autoe=std::make_tuple(1,2);auto&a=std::get(e);auto&b=std::get(e);但是,如果我打印出std::is_reference::value,我得到0在第一种情况下1在第二。这是为什么? 最佳答案 ifIprintoutstd::is_reference::value,Iget0inthefirstcase1int

c++ - 为什么 decltype 不是隐含的?

为什么decltype不能隐式添加到表达式中,而类型是预期的?templateautofoo(Xx,Yy,Zz){std::vectorvalues;//validinc++11/c++14//std::vectorvalues;//invalidvalues.push_back(x+y*z);returnvalues;//typededucedfromexpression-OK}在c++14中,编译器将能够根据返回表达式推断函数返回类型。为什么这不能扩展到任何“表达式->类型”转换?同样适用于declval,为什么我要写:std::vector()+declval()*declval

c++ - decltype() 可变模板基类

我在期待decltype()的地方有以下代码不工作Derived上课得到run()基类方法返回类型,因为基类没有默认构造函数。classBase{public:intrun(){return1;}protected:Base(int){}};structDerived:Base{templateDerived(Args...args):Base{args...}{}};intmain(){decltype(Derived{}.run())v{10};//itworks.Notexpectedsince//Derivedshouldnothavedefaultconstructorstd

C++ : convert vector to tuple

如何将std::vector转换为std::tuple?我有classT{};intcnt=3;vectortv;for(inti=0;i我想得到autotp=std::tie(*tv[0],*tv[1],*tv[2]);我怎样才能得到这个tp?如果cnt足够大,我不能手动写这个tp。std::vector,arma::mat>*>Conn1(6);for(size_ti=0;i,arma::mat>(inputLayer,*C1[i],*Conn1Opt[i],5,5));}这是代码。这里只有6个,但我还需要一些大小超过100的vector。我需要将这个vector转换为元组。