我有以下代码:
inline bool match(const std::wstring & text1, const std::wstring & text2)
{
return match(text1.c_str(), text2.c_str());
}
inline bool match(const std::wstring & text1, const wchar_t * text2)
{
return match(text1.c_str(), text2);
}
inline bool match(const wchar_t * text1, const std::wstring & text2)
{
return match(text1, text2.c_str());
}
inline bool match(const wchar_t * text1, const wchar_t * text2)
{
return !wcscmp(text1, text2);
}
Run Code Online (Sandbox Code Playgroud)
我得到:
error C2666: 'match' : 3 overloads have similar conversions
1> could be 'bool match(const wchar_t *,const std::wstring &)' …Run Code Online (Sandbox Code Playgroud) 我在不同的命名空间下有以下类。我的意思是,相同的 3 个类存在于不同的命名空间下。
public class A
{
public int a { get; set; }
}
public class B
{
public A objA { get; set; }
}
public class C
{
public List<B> listBinC { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
为了在这些类的对象之间利用/操作,我想到编写一个接口包装器,例如
public interface iA
{
int a { get; set; }
}
public interface iB<T> where T: iA
{
T objA { get; set; }
}
public interface iC<T> where T : iB<iA>
{
List<T> listBinC {get; set; }
}
Run Code Online (Sandbox Code Playgroud)
之后我将我的类定义更改为 …
我正在尝试使用 ScalaTest 进行基于属性的测试。我使用 2 个自定义生成器编写了一些测试用例,它们运行良好。然而,当我使用一个自定义生成器定义 forAll 时,如下所示:
it should "generate a given number of weights whose total is less than the given total when the given number is > 0, with default upperBound, lowerBound and total" in {
// TODO: Find out why single parameter does not work. maxSize here is a workaround
forAll(reasonableSizes) { (size: Int) =>
whenever(size > 0) {
val range = random.nextLong.abs
val values = DataGenerator.generatePartialWeights(size)
values should have length size
every(values) should (be >= BigDecimal(0) …Run Code Online (Sandbox Code Playgroud) 我是新手scala。我正在学习implicit variables。如何将隐式变量传递给调用另一个将使用该变量的函数的函数。我知道这个问题看起来很愚蠢。看看我写的代码就知道了。
class Person{
def whoAmI(implicit name: String): Unit = {
println(s"I am $name")
}
}
class NewPerson{
def whoAmI: Unit = {
val p: Person = new Person
p.whoAmI
}
}
object Main extends App {
implicit val name: String = "a Shamir"
val person: NewPerson = new NewPerson
person.whoAmI
}
Run Code Online (Sandbox Code Playgroud)
此代码不起作用。但这确实。
class Person{
def whoAmI(implicit name: String): Unit = {
println(s"I am $name")
}
}
class NewPerson{
implicit val name: String = "a Shamir" …Run Code Online (Sandbox Code Playgroud) 类似的问题之前已经被问过,例如String 文字匹配 bool 重载而不是 std::string。
但我想知道的是,C++ 开发人员应该怎样做才能防止这种情况发生?作为编写 C++ 库供其他人使用的人,我应该怎么做才能确保这种情况不会发生?这是我今天遇到的示例,其中一个库有 2 个initialize()方法:
void initialize(bool someflag) { /* ... */ }
void initialize(const std::string & name) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
现在有问题的代码位于想要利用此功能的应用程序中,并且以类似于以下的方式调用它:
initialize("robert");
Run Code Online (Sandbox Code Playgroud)
乍一看,您可能会认为这会调用initialize(string),但实际上它调用的是第一个initialize(bool),并将布尔标志设置为 true!
是的,我知道可以用以下方法解决:
initialize( std::string("robert") );
Run Code Online (Sandbox Code Playgroud)
但这让调用者承担了责任。
编辑@zdan:我不认为其他链接问题中的“解决方案”是很好的解决方案,因为1)我希望不必添加const char *采用布尔或字符串的每个方法的版本,2)模板解决方案显着提高了受影响方法的代码可维护性,使它们几乎不可读。
#include<stdio.h>
int fun(int (*x)[3]) {
return **x*2; // Why we need to dereference two times here?
}
int main(void) {
int a[3] = {1, 2, 3};
printf("%d", fun(a)); // 2
}
Run Code Online (Sandbox Code Playgroud)
所以我所知道的是数组名称仅作为数组的地址,并且在函数中 x 也期待数组的地址,所以为什么我们需要取消引用 x 两次而不是一次?
让我们假设我有这个代码:
class A {
public:
A(int y) { cout << y; }
};
int main() {
A a(1.5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
即使 1.5 是float,这段代码也能工作并返回 1。float被隐式转换为int。为什么不抛出异常?构造函数是否依赖于参数的数量或其类型?我在哪里可以阅读有关此内容的信息?
当隐式类声明中发生转换时,编译器未能选择正确的隐式转换方法。在下面的示例中,我有一个Foo[T]类和一个隐式Helper类,它采用 aFoo并提供一个print方法。该 print 方法调用show,它本身是一个由 on 隐式转换提供的方法Foo。
问题是有两种可能的转换提供show:一种转换Foo[T]为 a Bar[T],另一种转换Foo[Array[T]]为 a BarArray[T]。这个想法是,当我们有一个Foo包含数组的时候,我们想要应用更具体的BarArray转换。据我了解,编译器首先选择具有最具体类型的转换。
这在正常上下文中起作用,如下例所示,但print在隐式Helper类中的方法的上下文中中断。在那里,show调用了相同的方法,因此我希望应该应用相同的转换。然而,在这种情况下,编译器总是选择Bar转换,即使它有Foo[Array[T]]并且应该选择BarArray转换。
出了什么问题?
最小失败代码示例:
package scratch
import scala.language.implicitConversions
class Foo[T](val value: T) {}
object Foo {
implicit def fooToBar[T](foo: Foo[T]): Bar[T] = {
new Bar(foo.value)
}
implicit def fooArrayToBarArray[T](foo: Foo[Array[T]]): BarArray[T] = {
new …Run Code Online (Sandbox Code Playgroud) 例子:
struct c{
void operator=(bool){}
operator bool(){
return false;
}
c&operator=(const c&)=delete;
};
void f(bool){}
int main(){
c a,b;
f(b); //works fine
a=b; //g++ -std=c++17 says: error: use of deleted function ‘c& c::operator=(const c&)’
}
Run Code Online (Sandbox Code Playgroud)
为什么f(b)调用转换b为boolmatchf的类型但a=b坚持不转换?
考虑以下结构:
struct SomeWrapper
{
public Guid guid;
public static implicit operator SomeWrapper(Guid guid) => new SomeWrapper {guid = guid};
}
Run Code Online (Sandbox Code Playgroud)
这个结构定义了一个隐式运算符来处理Guidas SomeWrapper,非常简单。以下所有方法都可以编译,第一个除外PleaseDoNotCompile:
static Task<SomeWrapper> PleaseDoNotCompile() => Task.Run(() => Guid.NewGuid());
static Task<SomeWrapper> WhyDoYouCompile() => Task.Run(() =>
{
return Guid.NewGuid();
return new SomeWrapper();
});
static SomeWrapper IUnderstandWhyYouCompile() => Guid.NewGuid();
static async Task<SomeWrapper> IUnderstandWhyYouCompileToo() => await Task.Run(() => Guid.NewGuid());
Run Code Online (Sandbox Code Playgroud)
特别是,WhyDoYouCompile只是第一个带有返回SomeWrapper值的附加 return 语句的方法。很明显,返回是无法访问的代码。它仍然编译,而第一个没有。
因此,除了附加的 return 语句之外,这两种方法之间实际上还有另一个区别:PleaseDoNotCompileuses Task.Run<Guid>(Func<Guid> function)While WhyDoYouCompileuses Task.Run<SomeWrapper>(Func<SomeWrapper> function) …