我正在寻找一种找到隐式转换的方法.给我一个从一个类型转换为另一个类型的包的东西.
举个例子:
found : java.lang.String("")
required: net.liftweb.http.js.JsExp
Run Code Online (Sandbox Code Playgroud)
在Scaladoc中进行了一些搜索之后,我发现了包含隐式转换的包.是不是有一些自动化的搜索方式?
编辑:我不太擅长制定.我的问题是要找出我必须导入的包.使用隐式转换时,IDE已显示.但有时我相信某处隐式转换应该存在,但不知道哪个包.
可能重复:
为什么我的C++编译器不能推导出boost函数的模板参数?
std :: function的模板参数(签名)是不是它的类型?
我有以下内容:
#include <functional>
void Foo( std::function<void()> );
void Foo( std::function<void(int)> );
void Bar();
int main()
{
Foo( Bar ); // Error: ambiguous
Foo( [](){} ); // Error: ambiguous
Foo( std::function<void()>( Bar ) ); // Ok
Foo( std::function<void()>( [](){} ) ); // Ok
}
Run Code Online (Sandbox Code Playgroud)
我可以在main()中使用前两行而不使用最后两行中的函数样式转换吗?也许使用std :: enable_if解决方案?
在Youtube上的GoogleTechTalks视频中,Bjarne Stroustrup谈到即将推出的C++ 0x标准.在视频中,他提到了以下示例:
#include <iostream>
struct Sick
{
Sick(double d) { std::cout << d << "\n"; }
explicit Sick(int i) { std::cout << i << "\n"; }
};
int main()
{
Sick s1 = 2.1;
Sick s2(2.1);
}
Run Code Online (Sandbox Code Playgroud)
他是否意味着将explicit关键字放在之前Sick(double)而不是Sick(int),以突出显示与某些上下文中的隐式转换相关的问题?
当推断的返回类型是std :: nullptr_t时,为什么允许第二个赋值?使用函数指针,这是禁止的.
为什么第二个lambda不运行?
#include <cstdio>
#include <functional>
int main()
{
std::function<void* ()> f;
f = []() -> void* {
printf ("runs\n");
return nullptr;
};
f();
f = []() {
printf ("doesn't run\n");
return nullptr; // -> std::nullptr_t
};
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 是否可以使用带有varargs 的磁铁模式:
object Values {
implicit def fromInt (x : Int ) = Values()
implicit def fromInts(xs: Int*) = Values()
}
case class Values()
object Foo {
def bar(values: Values) {}
}
Foo.bar(0)
Foo.bar(1,2,3) // ! "error: too many arguments for method bar: (values: Values)Unit"
Run Code Online (Sandbox Code Playgroud)
?
对于Scala中的隐式转换,我可以使用隐式转换函数
implicit def intToX(i:Int):X = new X(i)
1.myMethod // -1
Run Code Online (Sandbox Code Playgroud)
或隐含的类
implicit class X(i: Int) {
def myMethod = - i
}
1.myMethod // -1
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别吗?我什么时候应该更喜欢一个?
关于隐式转换与类型类有一个相关的问题,但它只比较隐式函数和类型类.我感兴趣的是与隐式类的区别.
我正在尝试向现有类型添加新功能(因此我可以让IDE自动为我无法控制的类型建议相关功能,例如Future[Option[A]]).我已经探索了隐式类和隐式转换来实现这一点,它们似乎都提供了相同的行为.
使用隐式类之间是否有任何有效的区别:
case class Foo(a: Int)
implicit class EnrichedFoo(foo: Foo) {
def beep = "boop"
}
Foo(1).beep // "boop"
Run Code Online (Sandbox Code Playgroud)
并使用隐式转换:
case class Foo(a: Int)
trait Enriched {
def beep: String
}
implicit def fooToEnriched(foo: Foo) = new Enriched {
def beep = "boop"
}
Foo(1).beep // "boop"
Run Code Online (Sandbox Code Playgroud)
我想这里的一个区别可能是第一个例子创建了一个一个类而不是一个特征,但是我可以很容易地调整隐式类来扩展一个抽象特征,例如:
case class Foo(a: Int)
trait Enriched {
def beep: String
}
implicit class EnrichedFoo(foo: Foo) extends Enriched {
def beep = "boop"
}
Foo(1).beep // "boop"
Run Code Online (Sandbox Code Playgroud) 在这个问题1的启发下,我创建了以下代码:
struct X {
X(int) {}
};
struct Y {
operator X() { return X{1}; }
operator int() { return 1; }
};
int main() {
X x(Y{});
}
Run Code Online (Sandbox Code Playgroud)
这段代码:
如果启用C++ 11/14,所有GCC和Clang版本(我已尝试过)会引发歧义错误,
如果启用了C++ 17,则会在某些GCC和Clang版本中引发歧义错误(例如,GCC 8.2,GCC 6.3,Clang 5),
如果启用C++ 17,则编译一些GCC和Clang版本(例如,GCC 7.3,Clang 6).
歧义源于可能的转换:
Y→ X,Y→ int和int→ X,因为编译器不知道X它应该使用哪个构造函数(复制/移动或转换int).
我想知道为什么编译器行为存在这种差异,以及标准是否说该代码应该触发错误.
1请注意,这不是重复的.在相关问题中,OP询问了如何消除歧义.我问为什么不同编译器的行为不同.
从我的包装器类开始,Pointer<Base>我只想返回指向const的指针:Base const *.
当转换Pointer<Base>为Derived const *我得到编译错误:
错误C2440:'static_cast':'指针'无法转换为'const Derived*'
(翻译自德语VS2012)
struct Base { };
struct Derived : public Base { };
template <typename T>
class Pointer {
public:
Pointer(T *t = nullptr) : p(t) { }
//operator T*() { return p; }
operator T const *() const { return p; }
template <typename U>
inline U staticCast() const { return static_cast<U>(d); }
private:
T *p;
};
int main(int argc, char *argv[]) {
Derived …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用自定义整数类型,遇到了一个涉及泛型,隐式转换和32位整数的有趣问题。
下面是如何重现该问题的精简示例。如果我有两个隐式方法要转换int为MyInt,反之亦然,那么我会收到一个编译错误,看起来像C#无法解析要使用的泛型类型。并且仅在int或时发生uint。所有其它整数类型做工精细:sbyte,byte,short,ushort,long,ulong。
如果删除隐式转换方法之一,它也可以正常工作。与循环隐式转换有关?
using Xunit;
public class MyInt
{
public int Value;
//If I remove either one of the implicit methods below, it all works fine.
public static implicit operator int(MyInt myInt)
{
return myInt.Value;
}
public static implicit operator MyInt(int i)
{
return new MyInt() { Value = i };
}
public override bool …Run Code Online (Sandbox Code Playgroud)