根据此页面,您可以隐式转换shared_ptr<Foo>为shared_ptr<const Foo>.这很有道理.
但是,我碰到一个错误,当我尝试将转换std::vector包含shared_ptr<Foo>一个包含shared_ptr<const Foo>.
有没有一种很好的方法来实现这种转换?
我错过了haskell的便捷操作员$所以我决定介绍一个.
class Applayable[-R,T] (val host : Function[R,T]) {
def $: R=>T = host.apply
}
implicit def mkApplayable[R,T] (k : Function[R,T]) : Applayable[R,T] = new Applayable(k)
Run Code Online (Sandbox Code Playgroud)
它已经完全正常工作
val inc : Int => Int = _ + 1
inc $ 1
Run Code Online (Sandbox Code Playgroud)
但失败了
def inc(x:Int) : Int = x+1
inc $ 1
Run Code Online (Sandbox Code Playgroud)
我应该为隐式转换指定什么类型来将def定义转换为Applayable实例?
使用scala 2.9.2此代码:
BigDecimal(1) + new java.math.BigDecimal("1")
new java.math.BigDecimal("1") + BigDecimal(1)
不编译,因为scala.math.BigDecimal $#javaBigDecimal2bigDecimal未应用于第二种情况
但是,如果我在它之前定义相同的隐式权限,代码将编译:
BigDecimal(1) + new java.math.BigDecimal("1")
implicit def javaBigDecimal2bigDecimal(x: java.math.BigDecimal): BigDecimal = BigDecimal(x)
new java.math.BigDecimal("1") + BigDecimal(1)
为什么这样?
在我的应用程序中,我正在跟踪用户拥有的信用数量.要添加一些类型检查,我使用的Credits类与此类似:
case class Credits(val numCredits: Int) extends Ordered[Credits] {
...
}
Run Code Online (Sandbox Code Playgroud)
假设我有一个def accept(creds: Credits): Unit我想要调用的函数.有没有办法让我称之为
process(Credits(100))
process(0)
Run Code Online (Sandbox Code Playgroud)
但不是这个?
process(10)
Run Code Online (Sandbox Code Playgroud)
也就是说,我想仅从文字中提供隐式转换,0而不是其他.现在,我只是val Zero = Credits(0)在伴侣对象中,我认为这是一个相当不错的练习,但无论如何我都会对答案感兴趣,包括其他评论,例如:
Credits扩展AnyVal而不是2.10中的案例类?为什么C#不能将long var隐式转换为对象var然后转换为ulong?
long a = 0;
Object c = a;
ulong b = (ulong)c; // throw exception here
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
#include <iostream>
using namespace std;
int main()
{
string name = "John ";
int age = 32;
name += age;
cout << name << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码编译成功但在运行时背叛,因为它默默地忽略连接部分并打印:
John
Run Code Online (Sandbox Code Playgroud)
我知道我们需要使用stringstream来完成任务.但为什么上面的代码编译?因为以下代码:
#include <iostream>
using namespace std;
int main()
{
string name = "John ";
int age = 55;
name = name + age;
cout << name << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
适当抛出和错误:
错误:'name + age'中的'operator +'不匹配
我从Java中知道,与前一个构造a += b不同,a = a + b将结果类型化为a的类型. …
当我们执行以下操作时会发生什么:1)
int i = -1; // 32 bit
void *p;
p = reinterpret_cast<void*>(i)
Run Code Online (Sandbox Code Playgroud)
在64位架构上,sizeof(void*)== 8
2)
long long i; // 64 bit
void *p = (unsigned int)(-1);
i = retinterpret_cast<long long>(p)
Run Code Online (Sandbox Code Playgroud)
在32位架构sizeof(void*)= 4
我通常理解结果会是什么,但我希望有人能够根据C++标准来描述机制,以便更好地理解.
在第二种情况下,行为类似于"整数提升"(4.5)(我将为-1)中描述的情况"int - unsigned long",所以我们通常说指针转换为有符号整数?
3)
int i = ...;
unsigned long long L = ...;
i = L;
Run Code Online (Sandbox Code Playgroud)
这里有什么规则?
我刚写了这些代码:
int x = -1;//x must be negative
unsigned int y = 1;//y must be positive
bool b;
for(; ; x--, y++){
b = ((unsigned int)x) * y == ((unsigned int)(x * y));
}
Run Code Online (Sandbox Code Playgroud)
然后我发现这b总是如此.在我看来,((unsigned int)x)*y会溢出,但((unsigned int)(x*y))不会.我真的很难相信这是真的.这只是巧合还是这种现象背后有任何法律?
我试图了解隐式转换何时发生,并检测它们以提高代码库的质量。
我已经启用Wconversion,并Wsign-conversion为这个。但是我遇到了编译器没有给出任何错误的情况。例:
#include <iostream>
#include <array>
int main()
{
std::array<int, 10> vec{};
std::cout << vec[1] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
编译:
$ g++ --std=c++14 -Wall -Wextra -Werror -pedantic -Wsign-conversion -Wconversion test.cpp
$
Run Code Online (Sandbox Code Playgroud)
数组的大小和索引to operator[]都应为std::size_t(无符号)类型。但是,我正在传递带符号的文字,似乎没有问题。我什1.0F至可以传递给operator[]编译器。
operator[]但是,如果我为to的索引创建一个带符号的变量,则编译器会发出有关隐式转换的警告。
到底发生了什么事?使用文字时是否发生隐式转换?为什么编译器没有给出错误?我在Ubuntu 18.04上使用GCC 7.4。
在我的计算机上,long最大值为9223372036854775807。但是,当我unsigned long对较大的值感到厌倦时,编译器会发出警告,指出需要将其解释为unsigned long已经定义了该值。为什么会这样?
//assigning maximum value of a long integer. (No error)
long max_l = 9223372036854775807L;
//assigning an unsigned long integer.
unsigned long max_ul = 9223372036854775808L; //warning: integer literal is too large to be represented in a signed
//integer type, interpreting as unsigned [-Wimplicitly-unsigned-literal]
Run Code Online (Sandbox Code Playgroud)
cc --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Run Code Online (Sandbox Code Playgroud) c unsigned implicit-conversion unsigned-integer long-integer