草庐IT

type-inference

全部标签

c++ - 自动与字符串文字

#include#includeintmain(){constchara[]="helloworld";constchar*p="helloworld";autox="helloworld";if(typeid(x)==typeid(a))std::cout为什么x被推断为指针,而字符串文字实际上是数组?Anarrowstringliteralhastype"arrayofnconstchar"[2.14.5StringLiterals[lex.string]§8] 最佳答案 auto特性基于模板参数推导,模板参数推导的行为相同,特

c++ - 错误 C2893 : Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'

下面是一个给出编译时错误的程序。这主要与D类中的Boo函数有关。我最终尝试使用多个线程来调用solve方法,但目前这对我来说似乎不太有效,无法做到这一点。错误是:1>d:\dummy\project1\trash.cpp(37):warningC4101:'d':unreferencedlocalvariable1>c:\programfiles(x86)\microsoftvisualstudio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(240):errorC2672:'std::invoke':no

c++ - std::vector 应该尊重 alignof(value_type) 吗?

如果我定义一个具有特定对齐要求的简单类型,该类型的std::vector难道不应该为每个元素遵守对齐吗?考虑下面的例子typedefstd::arrayalignas(32)avx_point;std::vectorx(10);assert(!(std::ptrdiff_t(&(x[0]))&31)&&//assertthatx[0]is32-bytealigned!(std::ptrdiff_t(&(x[1]))&31));//assertthatx[1]is32-bytealigned我发现clang3.2(带或不带-stdlib=libc++)悄悄地(没有任何警告)违反了对齐要求

c++ - 振奋 spirit : What type names should be used for the built in terminals?

我正在重构一个类型系统(类型模型),它使用spirit进行字符串序列化。我正在使用类型特征的编译时建模构造。templatetype_traits{typedefboost::spirit::qi::int_parserstring_parser;}templatetype_traits{typedefboost::spirit::ascii::stringstring_parser;}在这个例子中,我展示了原始解析器,但我希望也加入规则。int4类型有效,但这是因为(home/qi/numeric/int.hpp+27):namespacetag{templatestructint_

c++ - 通过模板传递函数时推断的返回类型

我的问题是让编译器根据模板传递的函数的返回类型推断函数的返回类型。有什么方法可以调用吗foo(7.3)代替foo(7.3)在这个例子中:#includetemplateVfoo(Tt){returnfunc(t);}intbar(doublej){return(int)(j+1);}intmain(){printf("%d\n",foo(7.3));} 最佳答案 如果你想将bar保留为template参数,恐怕你只能接近它:#includetemplatestructtraits{};templatestructtraits{typ

c++ - C : x86 Intel Intrinsics usage of _mm_log2_ps() -> error: incompatible type 'int' ?

我正在尝试将log2应用于__m128变量。像这样:#includeintmain(void){__m128two_v={2.0,2.0,2.0,2.0};__m128log2_v=_mm_log2_ps(two_v);//log_2:=log(2)return0;}尝试编译会返回此错误:error:initializing'__m128'withanexpressionofincompatibletype'int'__m128log2_v=_mm_log2_ps(two_v);//log_2:=log(2)^~~~~~~~~~~~~~~~~~~我该如何解决?

python - c++中python "type(<name>, <bases>, <dict>)"的等价物是什么?

好吧,我正在将python3.3嵌入到C++应用程序中。我希望在C++端动态创建一个Python类,就像我在Python中执行以下操作一样:my_type=type("MyType",(object,),dict())我知道我总是可以导入“builtins”模块,但我一般会尽量避免在C++端导入。谢谢! 最佳答案 以下似乎工作得很好:PyObject*type(constchar*name,boost::python::tuplebases,boost::python::dictdict){returnPyType_Type.tp_

Elasticsearch:使用 Inference API 进行语义搜索

在我之前的文章“ElasticSearch8.12:让Lucene更快,让开发人员更快”,我有提到InferenceAPI。这些功能的核心部分始终是灵活的第三方模型管理,使客户能够利用当今市场上下载最多的向量数据库及其选择的转换器模型。在今天的文章中,我们将使用一个例子来展示如何使用InferenceAPI来进行语义搜索。前提条件你需要安装ElasticStack8.12及以上版本。你可以是自托管的Elasticsearch集群或者是在ElasticCloud上的部署由于OpenAI免费试用API的使用受到限制,因此需要付费OpenAI帐户才能将推理API与OpenAI服务结合使用。在今天的展

C++ 模板元编程 : how to deduce type in expression pattern

我想要静态检查lambda的参数类型。我在下面编写了这段代码,它似乎产生了正确的结果。structB{};autolamBc=[](Bconst&b){std::coutconstexprautoArgType(R(ClosureType::*)(Arg)const)->Arg;templateusingArgType_t=decltype(ArgType(&T::operator()));//ArgType_tis"referencetoBconst"但是,我注意到,例如,标准库使用类模板特化从std::remove_reference中的引用类型中提取引用类型。所以我尝试了这种方法

c++ - 具有复杂值类型 : confusion with value_type and reference 的迭代器

我想创建一个自定义迭代器包装器,例如enumerate:给定一对类型为T的迭代器,它会返回一个类型为std::pair的可迭代对象,其中该对的第一个元素将取值0、1、2,依此类推。我无法确定应该是什么value_type和reference我的迭代器。我想支持两种行为:首先,引用底层序列的值:for(auto&kv:enumerate(my_vec)){kv.second=kv.first;}(类似于std::iota);其次,复制值:std::vectora{10,20,30};autocopy=*enumerate(a).begin();a[0]=15;std::cout我很困惑I