我希望能够在两个不相容的类之间隐式转换.
其中一个类是Microsoft.Xna.Framework.Vector3,而另一个Vector类只是F#项目中使用的类.我正在使用XNA在C#中编写一个3D游戏,并且 - 虽然它是用3D绘制的,但游戏只在两个维度上进行(它是鸟瞰图).F#类使用2D矢量来处理物理:
type Vector<'t when 't :> SuperUnit<'t>> =
| Cartesian of 't * 't
| Polar of 't * float
member this.magnitude =
match this with
| Cartesian(x, y) -> x.newValue(sqrt (x.units ** 2.0 + y.units ** 2.0))
| Polar(m, _) -> m.newValue(m.units)
member this.direction =
match this with
| Cartesian(x, y) -> tan(y.units / x.units)
| Polar(_, d) -> d
member this.x =
match this with
| Cartesian(x, _) -> x
| …Run Code Online (Sandbox Code Playgroud) c# extension-methods f# operator-overloading implicit-conversion
我有一个ruby脚本,它将通过从另一个文件中获取和合并值来创建两个文件.
#Resources
require 'rubygems'
require 'csv'
col_date = []
col_constant1 = []
col_constant2 = []
col_appYear = []
col_statsDesc = []
col_keyStats =[]
col_weeklyTotal=[]
weekly_total = []
fname = "finalStats.csv" #variable for capture file
finalStatsFile = File.open(fname, "w") #write to capture file
fname2 = "weeklyStats.csv"
weeklyStatsFile = File.open(fname2, "w")
CSV.foreach('compareData.csv', converters: :numeric) do |row|
weekly_total << row[0] - row[1]
weekly_total.each do |data|
data << weekly_total.shift
weeklyStatsFile.puts data
end
end
#retrieve stats from original document
CSV.foreach("autoCapture.csv") {|row| col_date << row[0]}
CSV.foreach("autoCapture.csv") …Run Code Online (Sandbox Code Playgroud) 我想定义从(特定)lambda表达式到用户定义类型的隐式转换.我尝试了以下方法:
public static implicit operator DualElement<T>(Func<OPTatom, OPTatom, T> atomMap)
{
return new DualElement<T>(e => atomMap(e[0],e[1]));
}
Run Code Online (Sandbox Code Playgroud)
然后我试了一下
DualElement<double> dubidu = (i, j) => cost[i, j];
Run Code Online (Sandbox Code Playgroud)
它给出了"无法转换lambda表达式...因为它不是委托类型"
相反,有效的是:
DualElement<double> dideldu = (Func<OPTatom, OPTatom, double>)((i, j) => cost[i, j]);
Run Code Online (Sandbox Code Playgroud)
我猜,lambda表达式没有'Func'类型,所以我必须在隐式转换中加入不同的东西.
有人可以给我一个暗示吗?
考虑以下课程:
struct C
{
/* Class contents, without any arithmetic operator... */
constexpr operator int() noexcept; // Implicit conversion to int
};
Run Code Online (Sandbox Code Playgroud)
我的问题是:
std::sort当前使用默认<运算符的标准算法?LessThanComparable概念吗?LessThanComparable.给定一个简单的类模板,其中包含多个隐式转换函数(非显式构造函数和转换运算符),如以下示例所示:
template<class T>
class Foo
{
private:
T m_value;
public:
Foo();
Foo(const T& value):
m_value(value)
{
}
operator T() const {
return m_value;
}
bool operator==(const Foo<T>& other) const {
return m_value == other.m_value;
}
};
struct Bar
{
bool m;
bool operator==(const Bar& other) const {
return false;
}
};
int main(int argc, char *argv[])
{
Foo<bool> a (true);
bool b = false;
if(a == b) {
// This is ambiguous
}
Foo<int> c (1);
int d = 2; …Run Code Online (Sandbox Code Playgroud) 请在决定投票前重复阅读...
我有一个类型,实现了implicit cast另一种类型的运算符:
class A
{
private B b;
public static implicit operator B(A a) { return a.b; }
}
class B
{
}
Run Code Online (Sandbox Code Playgroud)
现在,隐式和显式转换工作正常:
B b = a;
B b2 = (B)a;
Run Code Online (Sandbox Code Playgroud)
......那么Linq怎么.Cast<>没有?
A[] aa = new A[]{...};
var bb = aa.Cast<B>(); //throws InvalidCastException
Run Code Online (Sandbox Code Playgroud)
看一下源代码.Cast<>,没有太大的魔力:如果参数真的是一个特殊情况IEnumerable<B>,那么:
foreach (object obj in source)
yield return (T)obj;
// ^^ this looks quite similar to the above B b2 = (B)a;
Run Code Online (Sandbox Code Playgroud)
那么为什么我的显式演员会工作,而不是里面的演员.Cast<>? …
假设我std::is_convertible无论出于何种原因并且想要自己实现它.该标准说明了以下几点:
is_convertible<From, To>当且仅当以下代码中的返回表达式格式正确时,才应满足模板特化的谓词条件,包括对函数返回类型的任何隐式转换:Run Code Online (Sandbox Code Playgroud)To f() { return declval<From>(); }
好吧,没什么大不了的,我可以这样做(注意参数顺序与之相反std::is_convertible,这是故意的,与问题无关):
template <typename To_, typename From_>
class my_is_convertible {
private:
template <typename To>
struct indirector {
indirector(To);
};
template <typename To, typename From>
struct tag {};
template <typename To, typename From>
static auto test(tag<To, From>)
-> decltype(indirector<To>(std::declval<From>()), std::true_type());
static auto test(...)
-> std::false_type;
public:
static constexpr bool value = decltype(test(tag<To_, From_>()))::value;
};
Run Code Online (Sandbox Code Playgroud)
这似乎按预期工作,据我所知,同样的事情.
现在我可以区分隐式和显式(或根本没有)构造函数:
struct A {};
struct B {};
struct Test {
Test(A);
explicit Test(B); …Run Code Online (Sandbox Code Playgroud) 我想知道以下代码是否导致未定义的行为:
#include <cstddef>
#include <cstdio>
struct IA {
virtual ~IA() {}
int a = 0;
};
struct IB {
virtual ~IB() {}
int b = 0;
};
struct C: IA, IB {};
int main() {
C* pc = nullptr;
IB* pib = pc;
std::printf("%p %p", (void*)pc, (void*)pib);
}
Run Code Online (Sandbox Code Playgroud) 该bool数据类型通常表示为0(如false)和1(如true)。但是,有人说true值可以用以外的值表示1。如果后面的语句是true,则以下表达式可能不正确。
bool x = 1;
if (x==1)
Do something..
Run Code Online (Sandbox Code Playgroud)
我想知道以下语句是否可以在常用的编译器上按期望和预期的方式工作。
bool x = 1;
if (x==1)
Do something.
Run Code Online (Sandbox Code Playgroud)
bool y = 0;
if (y>0.5)
Do something..
Run Code Online (Sandbox Code Playgroud)
bool z = 1;
if(z>0.5)
Do something...
Run Code Online (Sandbox Code Playgroud)当用作布尔表达式或显式或隐式转换为布尔值时,是否nullptr始终为假?该实现是否在标准中定义或指定?
我编写了一些代码进行测试,但不确定是否可以完全测试此属性。我找不到一个专门讨论此问题的现有SO答案。从我所见,cppreference并没有提到这一点。
if (nullptr) {
;
} else {
std::cout << "Evaluates to false implicitly\n";
}
if (!nullptr) {
std::cout << "Evaluates to false if operated on\n";
}
if (!(bool)(nullptr)) {
std::cout << "Evaluates to false if explicitly cast to bool\n";
}
Run Code Online (Sandbox Code Playgroud)
预期和实际:
Evaluates to false implicitly
Evaluates to false if operated on
Evaluates to false if explicitly cast to bool
Run Code Online (Sandbox Code Playgroud)