草庐IT

java - 为什么这里不进行隐式转换?

我希望将char数组转换为Set字符。从逻辑上讲,如果我写出类似HowtoconvertanArraytoaSetinJava的内容而不是使用它会工作的内置功能。但是,将内置函数与泛型一起使用则不会。TreeSetcharacterSet=Sets.newTreeSet();StringmyString="string";Character[]characterArray={'s','t','r','i','n','g'};Collections.addAll(characterSet,characterArray);//ThisworksCollections.addAll(char

java - 编译器何时不隐式使用 StringBuffer/StringBuilder?

我听说编译器(或者是JVM?)会自动使用StringBuilder进行一些字符串连接。什么时候才是明确宣布的合适时机?我不需要StringBuffer来实现线程安全。谢谢。 最佳答案 编译器会自动将它用于任何使用“+”的字符串连接。如果你想在一个循环中连接,你通常会显式地使用它。例如:StringBuilderbuilder=newStringBuilder();for(Stringname:names){builder.append(name);builder.append(",");}if(builder.length()>0)

java - 类声明中的成员接口(interface)是否隐式公开?

代码我有以下带有成员接口(interface)的类:packagecom.example.withinterface;publicclassSomeClass{interfaceSomeInterface{voiddoSomething();}}另一个试图访问它的类:packagecom.example.withinterface.main;importcom.example.withinterface.SomeClass;publicclassMain{publicstaticvoidmain(String[]argss){System.out.println(SomeClass.S

java - java运算符中的隐式转换+=

我发现javacompile在使用int和float的赋值和自赋值语句方面有一个非预期的行为。以下代码块说明了该错误。inti=3;floatf=0.1f;i+=f;//nocompileerror,buti=3i=i+f;//COMPILEERROR在自赋值i+=f中,编译不会出现错误,但计算结果是一个值为3的int,并且变量i保持值3。在i=i+f表达式中,编译器发出错误消息,并显示“错误:可能丢失精度”消息。谁能解释一下这种行为。编辑:我已经在https://compilr.com/cguedes/java-autoassignment-error/Program.java中发布

无法隐式转换类型的“ dbset< logs>'到“日志”

我在DBContextAPI方法页面中对以下模块有问题,我应该尝试将日志转换为日志吗?usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Http;usingSystem.Web.Http;namespaceLogging.Models{publicclassLog:DbContext{publicLog():base("name=LogContext"){Database.SetInitializer(null)

c++ - 为什么不在 placement new 中隐式调用析构函数”?

如本网站所引用...http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10但是我没有找到原因,为什么我们要显式调用析构函数? 最佳答案 您可以将其视为对delete的调用,但由于您使用了placementnew,因此您不想使用delete,因为那样会尝试释放内存。如果你想让它自动调用,你可以使用RAII://Coulduseatemplatedversion,orfindanexistingimplsomewhere:voiddestroy_fred(Fred*f){f-

c++ - 在 Qt 之外实现隐式共享类

我熟悉Qt使用D指针管理数据的方式。如何在我的代码中执行此操作?我试过这个方法:1)将所有数据移动到结构中2)添加一个QAtomicInt到结构3)实现一个=运算符并更改我的构造函数/解构函数以检查引用计数。问题是,当我去执行对象的浅拷贝时,我得到一个关于QObjectdeclaring=asprivate的错误。那我该如何实现呢?这是我的复制运算符的示例:HttpRequest&HttpRequest::operator=(constHttpRequest&other){other.d->ref.ref();if(!d->ref.deref())deleted;d=other.d;r

c++ - 将数字隐式转换为整型常量

假设我有这个功能:templatevoidfoo(std::integral_constant);现在要使用它,我会这样做:constexprsize_tmyNum=12;foo(std::integral_constant());但我想要一种像这样使用它的方法:constexprsize_tmyNum=12;foo(myNum);有没有什么方法可以将一个数字隐式转换为相应的std::integral_constant? 最佳答案 恐怕真正的隐式转换是不可能的。但是,您可以使用宏和编译时常量检测(请参阅https://stackov

c++ - 通过隐式转换为字符串流式传输对象时重载解析失败

免责声明:我知道应该避免隐式转换为字符串,正确的方法是opPerson过载.考虑以下代码:#include#include#includestructNameType{operatorstd::string(){return"wobble";}};structPerson{NameTypename;};intmain(){std::cout它yieldsthefollowingonGCC4.3.4:prog.cpp:Infunction‘intmain()’:prog.cpp:18:error:nomatchfor‘operator&std::basic_ostream::operat

c++ - Lambda 隐式捕获因从结构化绑定(bind)声明的变量而失败

使用以下代码,我得到一个编译错误C2065'a':undeclaredidentifier(使用visualstudio2017):[]{auto[a,b]=[]{returnstd::make_tuple(1,2);}();autor=[&]{returna;}();//errorC2065}();但是,下面的代码可以编译:[]{inta,b;std::tie(a,b)=[]{returnstd::make_tuple(1,2);}();autor=[&]{returna;}();}();我认为这两个样本是等价的。是编译器错误还是我遗漏了什么? 最佳答案