请参阅我对以下C#规范的一部分的剖析; 我想我必须遗漏一些东西,因为对我而言,我在这个问题中描述的行为实际上违反了规范.
好的,经过进一步的反思,并根据一些评论,我想我现在明白发生了什么.规范中的"源类型"一词指的是从中转换的类型- 即,Type2在我下面的例子中 - 这只是意味着编译器能够将候选缩小到两个定义的运算符(因为Type2它是源类型)对彼此而言).但是,它无法进一步缩小选择范围.所以规范中的关键词(适用于这个问题)是"源类型",我之前误解(我认为)是指"声明类型".
说我定义了这些类型:
class Type0
{
public string Value { get; private set; }
public Type0(string value)
{
Value = value;
}
}
class Type1 : Type0
{
public Type1(string value) : base(value) { }
public static implicit operator Type1(Type2 other)
{
return new Type1("Converted using Type1's operator.");
}
}
class Type2 : Type0
{
public Type2(string value) : base(value) { }
public …Run Code Online (Sandbox Code Playgroud) 拿这个代码:
class Register(var value:Int = 0) {
def getZeroFlag() : Boolean = (value & 0x80) != 0
}
object Register {
implicit def reg2int(r:Register):Int = r.value
implicit def bool2int(b:Boolean):Int = if (b) 1 else 0
}
Run Code Online (Sandbox Code Playgroud)
我想这样使用它:
val x = register.getZeroFlag + 10
Run Code Online (Sandbox Code Playgroud)
但我受到了欢迎:
type mismatch; found : Boolean required: Int
Run Code Online (Sandbox Code Playgroud)
怎么了?我是否需要定义一个隐式的函数返回一个bool?
在我的代码中的某个时刻,我想在A上调用C方法.这可能吗?我提出的一个修复是从A - > C转换,但这似乎有点多余.
注意:
谢谢 ...
我发现了我的程序的一个奇怪的行为,经过进一步的分析,我发现在我的C#知识或其他地方可能存在错误.我相信这是我的错,但我无法在任何地方找到答案......
public class B
{
public static implicit operator B(A values)
{
return null;
}
}
public class A { }
public class Program
{
static void Main(string[] args)
{
A a = new A();
B b = a ?? new B();
//b = null ... is it wrong that I expect b to be B() ?
}
}
Run Code Online (Sandbox Code Playgroud)
此代码中的变量"b"被计算为null.我不明白为什么它是null.
我用谷歌搜索并在这个问题中找到了一个响应- 使用官方规范隐式转换Null-Coalescing运算符结果.
但是按照这个规范,我找不到"b"为空的原因:(也许我读错了,在这种情况下我为垃圾邮件道歉.
如果A存在且不是可空类型或引用类型,则发生编译时错误.
......事实并非如此.
如果b是动态表达式,则结果类型是动态的.在运行时,首先评估a.如果a不为null,则a将转换为dynamic,这将成为结果.否则,评估b,结果成为结果.
......事实并非如此.
否则,如果A存在且是可空类型,并且存在从b到A0的隐式转换,则结果类型为A0.在运行时,首先评估a.如果a不为null,则打开a以键入A0,这将成为结果.否则,b被评估并转换为类型A0,这就成了结果.
...存在,从b到A0的隐式转换不存在.
否则,如果A存在且从b到A存在隐式转换,则结果类型为A.在运行时,首先计算a.如果a不为null,则a成为结果.否则,b被评估并转换为类型A,这就是结果.
...存在,从b到A的隐式转换不存在.
否则,如果b具有类型B并且从a到B存在隐式转换,则结果类型为B.在运行时,首先计算a.如果a不为null,则打开a以键入A0(如果A存在且可为空)并转换为类型B,这将成为结果.否则,b被评估并成为结果.
... b具有类型B,并且从a到B存在隐式转换.a被评估为null.因此,应评估b,结果应为b.
否则,a和b不兼容,并发生编译时错误.不会发生
我错过了什么吗?
关于以下编译器错误:
用户定义的运算符'Foo.implicit operator Foo(Bar)'必须声明为static和public
这是什么原因?为什么必须使用用户定义的转换运算符public?
提供以下代码,为什么这种转换不合法:
internal class Bar{}
internal class Foo
{
private Bar _myBar;
internal static implicit operator Bar(Foo foo)
{
return foo._myBar;
}
}
Run Code Online (Sandbox Code Playgroud)
我检查了C#语言规范,相关部分(10.10.3)没有提到这个要求.它是编译器而不是C#/ .Net限制吗?
为了使事情变得怪异,我可以作出上述转换操作符public,只要这两个Foo和Bar是internal,这是奇怪的,因为我认为你不能返回internal上一个公共方法的对象(虽然我想,如果整个类是internal它止跌没问题.但是,如果我创建public PublicFoo并保持Bar internal然后我得到inconsistent accessibility编译器错误:
internal class Bar { }
internal class Foo
{
private Bar _myBar;
/// <summary>
/// No inconsistent accessibility: return type
/// </summary>
public static implicit …Run Code Online (Sandbox Code Playgroud) 我想在返回时自动将值包装在通用容器中(我知道这并不总是可取的,但对我的情况来说这是有道理的).例如,我想写:
public static Wrapper<string> Load() {
return "";
}
Run Code Online (Sandbox Code Playgroud)
我可以通过在Wrapper类中添加以下内容来完成此操作:
public static implicit operator Wrapper<T>(T val) {
return new Wrapper<T>(val);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我尝试在IEnumerable这里(以及在ideone)转换完整代码时,这会失败:
public class Test {
public static void Main() {
string x = "";
Wrapper<string> xx = x;
string[] y = new[] { "" };
Wrapper<string[]> yy = y;
IEnumerable<string> z = new[] { "" };
Wrapper<IEnumerable<string>> zz = z; // (!)
}
}
public sealed class Wrapper<T> {
private readonly object _value;
public Wrapper(T …Run Code Online (Sandbox Code Playgroud) 使用g ++ 3.4和4.7,我观察到以下奇怪的行为:
如果需要用户定义的转换,则函数模板不匹配,其中普通函数将是.我在C++ 98标准中找不到相应的规则.g ++是否正确,(我假设)?或者这是一个错误?
template <class T>
int x(auto_ptr_ref<T> p)
{
return 1;
}
// this would match
/*
int x(auto_ptr_ref<int> p)
{
return 2;
}
*/
void dummy()
{
cout << x(auto_ptr<int>()) << endl;
}
Run Code Online (Sandbox Code Playgroud) c++ templates function overload-resolution implicit-conversion
我正面临MISRA C 2012违规,我无法理解.以下是代码:
#define I2C_CCRH_FS ((uint8_t)0x80)
#define I2C_CCRH_DUTY ((uint8_t)0x40)
#define I2C_CCRH_CCR ((uint8_t)0x0F)
typedef struct I2C_struct
{
volatile uint8_t CR1;
volatile uint8_t CR2;
volatile uint8_t CCRL;
volatile uint8_t CCRH;
} I2C_TypeDef;
#define I2C_BaseAddress 0x5210
#define I2C ((I2C_TypeDef *) I2C_BaseAddress)
I2C->CCRH &= ~(uint8_t)((I2C_CCRH_FS | I2C_CCRH_DUTY) | I2C_CCRH_CCR);
Run Code Online (Sandbox Code Playgroud)
在之前的代码中,PC-Lint抱怨说:
Unpermitted operand to operator '|' [MISRA 2012 Rule 10.1, required]
Mismatched essential type categories for binary operand [MISRA 2012 Rule 10.4, required]
Run Code Online (Sandbox Code Playgroud)
规则10.1规定ORing应该没有问题unsigned int.(PC-Lint通过第一次OR操作并抱怨第二次!)
规则10.4规定操作的操作数应具有相同的基本类型.
虽然所有操作数都被声明为,但我无法理解为什么存在这些违规行为uint8_t?
我试过在每两个ORed常量周围加上括号.我也尝试过将它们全部uint8_t …
我有这个编译时序列生成类(取自此处,并对其进行了一些修改):
#include <cstddef>
template <int...>
struct sequence {};
template <int... Ns>
struct generator;
template <std::size_t Count, int... Ns> // XXX (read below)
struct generator<Count, Ns...> {
using type = typename generator<Count - 1, Count - 1, Ns...>::type;
};
template <int... Ns>
struct generator<0, Ns...> {
using type = sequence<Ns...>;
};
template <std::size_t N>
using sequence_t = typename generator<N>::type;
int main() {
sequence_t<5> a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这在 Visual Studio 下编译得很好(即使使用/permissive-开关)。但在 GCC 下会抛出错误:
#include <cstddef>
template <int...> …Run Code Online (Sandbox Code Playgroud) c++ template-specialization language-lawyer implicit-conversion variadic-templates
我正在努力使最简单的示例发挥作用。这是我的代码:
// example.cpp
#include <pybind11/pybind11.h>
namespace py = pybind11;
class B {
public:
int b;
};
class A {
public:
int a;
A(int a) :a(a) {}
A(B b) { a = b.b; }
};
void fn(A) {}
PYBIND11_MODULE(example, m) {
py::class_<A>(m, "A")
.def(
py::init<int>(),
py::arg("a") = 1
);
py::class_<B>(m, "B")
.def(
py::init<int>(),
py::arg("b") = 2
);
py::implicitly_convertible<A, B>();
m.def("fn", &fn,
py::arg("a")
);
}
Run Code Online (Sandbox Code Playgroud)
# test.py
from example import *
a = A()
b = B()
fn(b)
Run Code Online (Sandbox Code Playgroud)
它构建得很好,但输出是:
$ python3.9 test.py …Run Code Online (Sandbox Code Playgroud)