pimp-my-library模式允许我通过提供从该类到实现该方法的类的隐式转换,似乎向类添加方法.
斯卡拉不允许两个这样的隐式转换正在发生,但是,我不能得到A到C使用隐含A于B和另一个隐含的B对C.有没有办法解决这个限制?
令我惊讶的是,这编译:
const char* c_str()
{
static const char nullchar = '\0';
return nullchar;
}
Run Code Online (Sandbox Code Playgroud)
它在我的代码中引入了一个错误.谢天谢地,我抓住了它.
这是故意的C++,还是编译器错误?是否有理由主动忽略数据类型?
它适用于Visual C++ 2010和GCC,但我不明白为什么它应该工作,因为明显的数据类型不匹配.(static也没必要.)
例如,以下查询工作正常:
SELECT *
FROM quotes
WHERE expires_at <= '2010-10-15 10:00:00';
Run Code Online (Sandbox Code Playgroud)
但这显然是在执行'字符串'比较 - 我想知道是否有一个内置于MySQL的函数专门进行'datetime'比较.
我认为这个问题会让我在Stack Overflow上立刻成名.
假设您有以下类型:
// represents a decimal number with at most two decimal places after the period
struct NumberFixedPoint2
{
decimal number;
// an integer has no fractional part; can convert to this type
public static implicit operator NumberFixedPoint2(int integer)
{
return new NumberFixedPoint2 { number = integer };
}
// this type is a decimal number; can convert to System.Decimal
public static implicit operator decimal(NumberFixedPoint2 nfp2)
{
return nfp2.number;
}
/* will add more nice members later */
} …Run Code Online (Sandbox Code Playgroud) 说我正在写一个扩展方法
implicit class EnhancedFoo(foo: Foo) {
def bar() { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)
你应该总是extends AnyVal在课堂上定义吗?在什么情况下你不想让隐式类成为一个值类?
我有一个泛型类,我正在尝试实现隐式类型转换.虽然它主要起作用,但它不适用于界面转换.经过进一步调查,我发现存在编译器错误:"来自接口的用户定义转换"适用.虽然我知道在某些情况下应该强制执行,但我正在尝试做的事情似乎是合法的案例.
这是一个例子:
public class Foo<T> where T : IBar
{
private readonly T instance;
public Foo(T instance)
{
this.instance = instance;
}
public T Instance
{
get { return instance; }
}
public static implicit operator Foo<T>(T instance)
{
return new Foo<T>(instance);
}
}
Run Code Online (Sandbox Code Playgroud)
使用它的代码:
var concreteReferenceToBar = new ConcreteBar();
IBar intefaceReferenceToBar = concreteReferenceToBar;
Foo<ConcreteBar> concreteFooFromConcreteBar = concreteReferenceToBar;
Foo<IBar> fooFromConcreteBar = concreteReferenceToBar;
Foo<IBar> fooFromInterfaceBar = intefaceReferenceToBar; // doesn't work
Run Code Online (Sandbox Code Playgroud)
有没有人知道一个解决方法,或者任何人都能以令人满意的方式解释为什么我不能施展 interfaceReferenceToBar隐式地Foo<IBar>,因为在我的情况下它没有被转换,只包含在Foo中?
编辑: 看起来协方差可能提供救赎.我们希望C#4.0规范允许使用协方差隐式转换接口类型.
c# compiler-construction generics casting implicit-conversion
指向非const数据的指针可以隐式转换为指向相同类型的const数据的指针:
int *x = NULL;
int const *y = x;
Run Code Online (Sandbox Code Playgroud)
添加额外的const限定符以匹配额外的间接寻址应该在逻辑上以相同的方式工作:
int * *x = NULL;
int *const *y = x; /* okay */
int const *const *z = y; /* warning */
Run Code Online (Sandbox Code Playgroud)
-Wall但是,使用标志对GCC或Clang进行编译会产生以下警告:
test.c:4:23: warning: initializing 'int const *const *' with an expression of type
'int *const *' discards qualifiers in nested pointer types
int const *const *z = y; /* warning */
^ ~
Run Code Online (Sandbox Code Playgroud)
为什么添加额外的const限定符"在嵌套指针类型中丢弃限定符"?
我知道你可以使用C++关键字'explicit'作为类的构造函数来防止类型的自动转换.您是否可以使用相同的命令来阻止类方法的参数转换?
我有两个类成员,一个将bool作为参数,另一个是unsigned int.当我用int调用函数时,编译器将param转换为bool并调用错误的方法.我知道最终我将替换bool,但是现在不想破坏其他例程,因为这个新例程已经开发出来了.
这是一种我无法理解的怪异行为.在我的例子我有一个类Sample<T>和隐式转换操作符T来Sample<T>.
private class Sample<T>
{
public readonly T Value;
public Sample(T value)
{
Value = value;
}
public static implicit operator Sample<T>(T value) => new Sample<T>(value);
}
Run Code Online (Sandbox Code Playgroud)
使用空值类型为出现问题时,T例如int?.
{
int? a = 3;
Sample<int> sampleA = a;
}
Run Code Online (Sandbox Code Playgroud)
这里是关键部分:
在我看来,这不应该编译,因为Sample<int>定义了一个转换,从int以Sample<int>而不是从int?到Sample<int>.但它编译并成功运行!(我的意思是调用转换运算符3并将其分配给该readonly字段.)
它变得更糟.这里不调用转换运算符,sampleB将其设置为null:
{
int? b = null;
Sample<int> sampleB = b; …Run Code Online (Sandbox Code Playgroud) 本文旨在用作关于C中隐式整数提升的常见问题解答,特别是由通常的算术转换和/或整数提升引起的隐式提升.
示例1)
为什么这会给出一个奇怪的大整数而不是255?
unsigned char x = 0;
unsigned char y = 1;
printf("%u\n", x - y);
Run Code Online (Sandbox Code Playgroud)
例2)
为什么这会给"-1大于0"?
unsigned int a = 1;
signed int b = -2;
if(a + b > 0)
puts("-1 is larger than 0");
Run Code Online (Sandbox Code Playgroud)
示例3)
为什么更改上例中的类型来short解决问题?
unsigned short a = 1;
signed short b = -2;
if(a + b > 0)
puts("-1 is larger than 0"); // will not print
Run Code Online (Sandbox Code Playgroud)
(这些示例适用于16位或短16位的32位或64位计算机.)
c# ×3
c ×2
c++ ×2
generics ×2
nullable ×2
scala ×2
.net ×1
casting ×1
compiler-bug ×1
const ×1
explicit ×1
implicits ×1
mysql ×1
qualifiers ×1
sql ×1
type-safety ×1
value-type ×1