例如,以下查询工作正常:
SELECT *
FROM quotes
WHERE expires_at <= '2010-10-15 10:00:00';
Run Code Online (Sandbox Code Playgroud)
但这显然是在执行'字符串'比较 - 我想知道是否有一个内置于MySQL的函数专门进行'datetime'比较.
考虑以下程序:
#include <iostream>
class A
{
public:
A( ) { std::cout << "A()\n"; }
A( A& ) = delete;
A( int i ) { std::cout << "A( " << i << " )\n"; }
explicit operator int( ) { std::cout << "operator int()\n"; return 42; }
};
template< typename T = A > void f( T a = A() ) {}
int main( void )
{
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2013编译此代码并使用输出运行
A()
operator int()
A( 42 )
Run Code Online (Sandbox Code Playgroud)
这是编译器错误吗?看起来VS编译器在此上下文中没有注意到'explicit'关键字.根据我的理解,VS …
为什么switch和if语句在转换运算符方面表现不同?
struct WrapperA
{
explicit operator bool() { return false; }
};
struct WrapperB
{
explicit operator int() { return 0; }
};
int main()
{
WrapperA wrapper_a;
if (wrapper_a) { /** this line compiles **/ }
WrapperB wrapper_b;
switch (wrapper_b) { /** this line does NOT compile **/ }
}
Run Code Online (Sandbox Code Playgroud)
编译错误是switch quantity is not an integer在if声明中它被完美地识别为a bool.(GCC)
c++ if-statement switch-statement conversion-operator explicit-conversion
这是Explicit ref-qualified转换运算符模板的后续操作.我已经尝试了许多不同的选项,我在这里给出了一些结果,试图看看最终是否有任何解决方案.
假设一个类(例如任何一个)需要以方便,安全(无意外)的方式提供转换到任何可能的类型,以保留移动语义.我可以想到四种不同的方式.
struct A
{
// explicit conversion operators (nice, safe?)
template<typename T> explicit operator T&& () &&;
template<typename T> explicit operator T& () &;
template<typename T> explicit operator const T& () const&;
// explicit member function (ugly, safe)
template<typename T> T&& cast() &&;
template<typename T> T& cast() &;
template<typename T> const T& cast() const&;
};
// explicit non-member function (ugly, safe)
template<typename T> T&& cast(A&&);
template<typename T> T& cast(A&);
template<typename T> const …Run Code Online (Sandbox Code Playgroud) c++ implicit-conversion move-semantics explicit-conversion c++11
我想使用反射,并使用反射进行隐式或显式转换.
鉴于我已经用这种方式定义了Foo
public class Foo
{
public static explicit operator decimal(Foo foo)
{
return foo.Value;
}
public static explicit operator Foo(decimal number)
{
return new Foo(number);
}
public Foo() { }
public Foo(decimal number)
{
Value = number;
}
public decimal Value { get; set; }
public override string ToString()
{
return Value.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时
decimal someNumber = 42.42m;
var test = (Foo)someNumber;
Console.WriteLine(test); // Writes 42.42 No problems
Run Code Online (Sandbox Code Playgroud)
当我尝试使用Foo定义一个类作为成员类型并使用反射来设置它时.我得到以下例外.
Error : Object of type 'System.Decimal' cannot be converted …Run Code Online (Sandbox Code Playgroud) 我从另一个问题(稍加修改)中借用了以下代码,以便在我的代码中使用:
internal class PositiveDouble
{
private double _value;
public PositiveDouble(double val)
{
if (val < 0)
throw new ArgumentOutOfRangeException("Value needs to be positive");
_value = val;
}
// This conversion is safe, we can make it implicit
public static implicit operator double(PositiveDouble d)
{
return d._value;
}
// This conversion is not always safe, so we're supposed to make it explicit
public static explicit operator PositiveDouble(double d)
{
return new PositiveDouble(d); // this constructor might throw exception
} …Run Code Online (Sandbox Code Playgroud) 在此代码中,分配给b1有效,但它不允许分配给b2(有或没有静态强制转换).我实际上是试图解决相反的问题,公共继承但不是隐式转换为基础.然而似乎从未使用过演员.为什么是这样?
struct B {};
struct D1 : private B {
operator B&() {return *this;}
B& getB() {return *this;}
};
struct D2 : public B {
explicit operator B&() {return *this;}
};
struct D3 : public B {
operator B&() = delete;
};
void funB(B& b){}
int main () {
D1 d1;
funB(d1.getB()); // works
// funB(d1); // fails to compile with 'inaccessible base class
D2 d2;
funB(d2); // works
D3 d3;
funB(d3); // works
return 0;
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
隐式VS显式转换
"隐式转换"和"显式转换"之间有什么区别?Java和C++的区别是什么?
C#的一个鲜为人知的特性是可以创建隐式或显式的用户定义类型转换.我已经写了6年的C#代码了,我从来没有用过它.所以,我担心我可能错过了很好的机会.
什么是用户定义转换的合法,良好用途?您是否有比仅定义自定义方法更好的示例?
-
事实证明,微软有一些关于转换的设计指南,其中最相关的是:
如果最终用户未明确预期此类转换,请勿提供转换运算符.
但什么时候转换"预期"?在玩具编号课程之外,我无法弄清楚任何真实世界的用例.
以下是答案中提供的示例摘要:
模式似乎是:隐式转换大多数(仅?)在定义数值/值类型时很有用,转换由公式定义.回想起来,这是显而易见的.不过,我想知道非数字类是否也可以从隐式转换中受益..?
c# casting operator-overloading implicit-conversion explicit-conversion
sdr是我的sqldatareader,我想检查一个十进制类型的curPrice值是否为null.
inrec.curPrice = sdr.IsDBNull(7) ? (decimal?)null : sdr.GetDecimal(7);
这是我收到的错误消息:
无法隐式转换类型'decimal?' 到'十进制'.存在显式转换(您是否错过了演员?)
哪里出错了,请有人告诉我.