这就是我想要做的......
public class A
{
public string Content { get; set; }
}
A a = new A();
a.Content = "Hello world!";
string b = a; // b now equals "<span>Hello world!</span>"
Run Code Online (Sandbox Code Playgroud)
所以我想控制如何 a转换成String......不知怎的......
Scala 2.8规范在7.3节中说明(突出显示是我的):
隐式参数和方法还可以定义称为视图的隐式转换.从类型S到类型T的视图由具有函数类型S => T或(=> S)=> T的隐式值或可转换为该类型的值的方法定义.视图适用于两种情况.
- 如果表达式e是T类型,并且T 不符合表达式的预期类型pt.在这种情况下,搜索隐式v,其适用于e并且其结果类型符合 pt.搜索按照隐式参数的情况进行,其中隐式范围是T => pt.如果找到这样的视图,则表达式e被转换为v(e).
[...]
鉴于以上事项和以下事实:
Long不是子类型java.lang.Comparable[Long],即不符合类型的T地方T <: java.lang.Comaparable[Long]Predef 包含 implicit def longWrapper (x: Long) : RichLongRichLong是一个子类型java.lang.Comparable[Long],即符合类型TwhereT <: java.lang.Comaparable[Long]我希望隐式转换应用于Long遇到的位置和java.lang.Comparable[Long]预期的子类型.然而:
scala> def test[T <: java.lang.Comparable[Long]](c: T) = println(c)
test: [T <: java.lang.Comparable[Long]](c: T)Unit
scala> test(12L)
<console>:7: error: inferred type arguments [Long] do not conform to method …Run Code Online (Sandbox Code Playgroud) 我的目标是设计一个String类来装饰std :: string,以便提供我的程序所需的一些功能.我想要添加的一个功能是隐式地将任何东西转换为我的String,以节省一些输入.
为了实现隐式转换,我设计了以下类:
std::ostream& operator<<(std::ostream& o, const String& s);
class String {
public:
template<typename t_value>
String::String(t_value value) {
std::ostringstream oss;
oss << value;
_str = oss.str();
}
private:
std::string _str;
}
Run Code Online (Sandbox Code Playgroud)
这适用于具有<<运算符定义的任何类型.任何没有流操作符的类都会出现问题.编译器错误会很好,但我得到的是无限递归,因为C++试图使用我的全局<<运算符来尝试转换为我的String类型.
我的主要目标是像这样编码
class Foo {
int _memberWithUnderscoreInName;
}
String s = Foo();
Run Code Online (Sandbox Code Playgroud)
并在构造函数中获得编译器错误而不是无限循环.
有一个简单的解决方案吗?
请考虑以下代码:
#include <string>
class WrapperString
{
public:
WrapperString(const std::string& str) : str_(str) {}
operator std::string() const { return str_; }
operator const std::string() const { return str_; }
std::string get() const { return str_; }
// error C2373: 'WrapperString::get' : redefinition; different type modifiers
// const std::string get() const { return str_; }
private:
std::string str_;
};
int main (int, char *[])
{
WrapperString w( "Hello" );
std::string foo = w; // disabling either implicit conversion makes this work
return …Run Code Online (Sandbox Code Playgroud) 我有一个(成员)功能重载像这样:
bool foo(bool);
int foo(int);
float foo(float);
...
std::string foo( std::string const&);
Run Code Online (Sandbox Code Playgroud)
对于几种内置类型但不适用于const char*.打电话foo("beauty is only skin-deep");,我的大惊喜,叫foo的函数的布尔变.这引出了我的问题:
问题:是否为内置类型定义了明确的隐式转换顺序
不是问题:如何避免隐式转换.隐含转换是多么邪恶....
编辑:删除了有关用户定义问题的隐式转换顺序的问题
请考虑以下代码:
struct Bar{};
struct Foo
{
Foo() = default;
Foo(const Bar&) {}
Foo(const Foo&) = delete;
// IMPLICIT conversion to Bar
operator Bar(){return {};}
};
int main()
{
Foo f1;
Foo f2(static_cast<Bar>(f1)); // this is OK
Foo f3(f1); // does not compile, why not implicit conversion to `Bar`?
}
Run Code Online (Sandbox Code Playgroud)
该类Bar有一个用户定义的转换运算符Foo,它接受Bar&s.但是,在最后一行中main,我原本希望Foo f1转换为a Bar然后传递给Foo(const Bar&).但是,只Foo(const Foo&) = delete;考虑删除的构造函数.我知道这个构造函数是一个更好的匹配,但为什么不在Foo(const Bar&)重载集中也是为什么编译器不执行隐式转换?
#include <iostream>
struct int_wrapper
{
int i;
int_wrapper(int i = 0) : i(i) {}
};
struct A
{
int_wrapper i;
int_wrapper j;
A(int_wrapper i = 0, int_wrapper j = 0) : i(i), j(j) {}
};
int main()
{
A a;
a = {3};
// error: no match for ‘operator=’ (operand types are ‘A’ and ‘int’)
// a = 3;
std::cout << a.i.i << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道在进行隐式转换时不允许多个用户转换,但是,为什么使用brace-init-list双重用户转换有效?
考虑以下代码:
#include <cstdint>
class A
{
public:
explicit A(uint8_t p_a){ m_a = p_a; };
uint8_t get_a() const {return m_a;}
private:
uint8_t m_a;
};
int main()
{
A a {0x21U};
A aa{0x55U};
uint8_t mask{a.get_a() | aa.get_a()};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译this(gcc 5.4.0)时,我收到以下错误:
main.cpp: In function ‘int main()’:
main.cpp:20:28: warning: narrowing conversion of ‘(int)(a.A::get_a() | aa.A::get_a())’ from ‘int’ to ‘uint8_t {aka unsigned char}’ inside { } [-Wnarrowing]
uint8_t mask{a.get_a() | aa.get_a()};
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么会有任何缩小.该int类型从未在我的代码中的任何地方使用过,所有内容都是用unsigned chars语言编写的.即使我明确地转向unsigned char我得到错误:
uint8_t …Run Code Online (Sandbox Code Playgroud) 我有两个特点:
trait Foo {}
trait Bar {}
struct FooImpl;
impl Foo for FooImpl {}
struct BarImpl;
impl Bar for BarImpl {}
Run Code Online (Sandbox Code Playgroud)
我要转换成第三种类型:
struct Baz;
trait IntoBaz {
fn into(self) -> Baz;
}
Run Code Online (Sandbox Code Playgroud)
由于连贯性,我无法为两个特征定义两个impls IntoBaz,所以我换了一个:
struct FooWrapper<F>(F)
where
F: Sized;
impl<F: Foo + Sized> From<F> for FooWrapper<F> {
fn from(f: F) -> FooWrapper<F> {
FooWrapper(f)
}
}
impl<F: Foo + Sized> IntoBaz for FooWrapper<F> {
fn into(self) -> Baz {
Baz
}
}
Run Code Online (Sandbox Code Playgroud)
我不包装另一个:
impl<B: Bar> IntoBaz for …Run Code Online (Sandbox Code Playgroud) 我试图在C ++中找到与Java的in.hasNextInt等效的输入,我发现了这一点。
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
int myint;
std::cout << "Please enter some integers (enter 0 to end):\n";
do {
std::cin >> myint;
myvector.push_back (myint);
} while (myint);
std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我真的不明白为什么当输入为0时输入循环会停止。它的while(myint)循环也使我感到困惑,因为myint它是整数而不是布尔值。当我们输入其他内容时,它可以作为布尔值工作,myint但我认为0仍然可以作为整数。有人可以向我解释一下吗?