我有一个简单的场景,可能会或可能不会.我有一个包含整数的类,为此我会尽可能简单:
public class Number
{
public int Value {get; set;}
public string Name {get; set;}
}
public static void Print(int print)
{
Console.WriteLine(print);
}
public static string Test()
{
Number num = new Number (9, "Nine");
Print(num); //num "overloads" by passing its integer Value to Print.
}
// Result
// 9
Run Code Online (Sandbox Code Playgroud)
如何使Test()函数正常工作,因为我编写了它?这甚至可能吗?我认为这可以使用显式/隐式运算符来完成,但我无法弄明白.
我有一个结构:
public struct Decibel
{
public readonly double Value;
public Decibel (double value)
{
Value = value;
}
public static implicit operator Decibel (double decibels)
{
return new Decibel (decibels);
}
public static implicit operator double (Decibel decibels)
{
return decibels.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我可以这样做:
bool x = new Decibel (0) < new Decibel (1);
Run Code Online (Sandbox Code Playgroud)
似乎编译器足够智能转换Decibel为double,然后使用<运算符进行双打?
我有不同的struct名字Duration,包装TimeSpan.它有隐式转换为TimeSpan向/从Duration,但<和>运营商都没有工作了.
c#只识别原始类型之间的转换吗?
我喜欢隐含的定义.它们使代码看起来很好,它们使用户觉得某些特性在类只是一个隐式定义时自然可用.然而,我在考虑JS原型,你可以基本上在你没写的类上定义一个方法.但是如果在下一个版本中,这个类定义了一个具有相同签名的方法并对其行为做出假设,那么你就搞砸了.
Scala的含义使得几乎完全相同,只有一个主要区别:隐式定义是作用域的,因此类的作者不会有通过其他人的代码中的隐式定义注入代码的风险.但是用户的代码怎么样?是否可以保护他免受课堂上的变化?
我们考虑一下这段代码:
class HeyMan {
def hello = println("Hello")
}
object Main extends App {
val heyMan = new HeyMan
implicit class ImplicitHeyMan(heyMan: HeyMan) {
def hello = println("What's up ?")
}
heyMan.hello // prints Hello
}
Run Code Online (Sandbox Code Playgroud)
很糟糕,不是吗?对我来说,正确的行为应该是隐式定义总是隐藏真实定义,以便保护用户代码免受他所调用的API中新方法的出现.
你怎么看 ?有没有办法让它安全或者我们应该停止使用这种方式吗?
我最近读过一些关于使用大括号来允许多个用户定义转换的地方(不记得在哪里),但是构造函数转换和转换方法转换之间似乎存在差异,我不明白.
考虑:
#include <string>
using ::std::string;
struct C {
C() {}
};
struct A {
A(const string& s) {} // Make std::string convertible to A.
operator C() const { return C(); } // Makes A convertible to C.
};
struct B {
B() {}
B(const A& a) {} // Makes A convertible to B.
};
int main() {
B b;
C c;
// This works.
// Conversion chain (all thru ctors): char* -> string -> A -> B
b = …Run Code Online (Sandbox Code Playgroud) class A
{
struct B{};
public:
static void test(A::B){}
};
struct C
{
template<class T>
operator T()
{
return T();
}
};
int main()
{
A::test(C());
}
Run Code Online (Sandbox Code Playgroud)
此代码适用于clang 3.7,gcc 5.1和vc ++ 14.2.
2问题,
1.为什么模板推导出的类型是A :: B?(太聪明了!)
据我所知,模板通过return语句而不是参数来推断类型.
但是我发现了一些对N4606 12.3.2感兴趣的东西A conversion function template shall not have a deduced return type (7.1.7.4).(但是,我找不到关于此的更多信息,因为7.1.7.4太难理解了.)
2.为什么转换函数模板可以访问A :: B?
谢谢.
可能重复:
为什么我的C++编译器不能推导出boost函数的模板参数?
std :: function的模板参数(签名)是不是它的类型?
我有以下内容:
#include <functional>
void Foo( std::function<void()> );
void Foo( std::function<void(int)> );
void Bar();
int main()
{
Foo( Bar ); // Error: ambiguous
Foo( [](){} ); // Error: ambiguous
Foo( std::function<void()>( Bar ) ); // Ok
Foo( std::function<void()>( [](){} ) ); // Ok
}
Run Code Online (Sandbox Code Playgroud)
我可以在main()中使用前两行而不使用最后两行中的函数样式转换吗?也许使用std :: enable_if解决方案?
当推断的返回类型是std :: nullptr_t时,为什么允许第二个赋值?使用函数指针,这是禁止的.
为什么第二个lambda不运行?
#include <cstdio>
#include <functional>
int main()
{
std::function<void* ()> f;
f = []() -> void* {
printf ("runs\n");
return nullptr;
};
f();
f = []() {
printf ("doesn't run\n");
return nullptr; // -> std::nullptr_t
};
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在以下代码中,f(int)选择重载而不是f(unsigned).用clang 3.0和gcc 4.8测试.
enum E
{
};
E f(int);
int f(unsigned);
E e = f(E(0));
Run Code Online (Sandbox Code Playgroud)
我对该标准的阅读使我认为enum -> int并且enum -> unsigned是相同的标准转换序列,它们都只包含整数转换.
[conv.integral]枚举类型的右值可以转换为整数类型的右值.
根据[over.best.ics],仅包含积分转换的标准转换序列的等级是"转换".
[over.ics.rank]两个相同形式的隐式转换序列是无法区分的转换序列,除非以下规则之一适用:[...]
在比较两个标准转换序列时,所提到的规则似乎都不适用.
我错过了什么?
为什么gcc需要复制构造函数来进行隐式转换构造函数调用?
class X
{
public:
X(int q) {}
~X()
{
std::cout << "~X()" << std::endl;
}
X(const X&) = delete;
};
X x = 1; // gives error: use of deleted function ‘X::X(const X&)’
Run Code Online (Sandbox Code Playgroud)
更有趣的是,如果我甚至写复制构造函数,它就不会被调用.析构函数只调用一次,因此以下代码
class X
{
public:
X(int q) {}
~X()
{
std::cout << "~X()" << std::endl;
}
X(const X&)
{
std::cout << "copy ctor" << std::endl;
}
};
int main()
{
X x = 1;
}
Run Code Online (Sandbox Code Playgroud)
版画 ~X()
是bug吗?有没有解决方法?
我的locaL PC上的gcc版本是4.6.3,这在另一个gcc版本上也是一样的(在线)
我找不到Scala代码中使用的隐式转换或隐式参数值.这使得阅读开源项目非常困惑.
这个implicitly技巧只能帮助检查是否存在某种类型的有效隐式变量,但无法确定隐含变量的定义位置.
scala> implicitly[(Int) => RichInt]
res2: Int => scala.runtime.RichInt = <function1>
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以找到一段代码所使用的隐式转换和值的定义?如果源代码文件很长,那将是一项巨大的工作.
c++ ×6
c++11 ×3
c# ×2
overloading ×2
scala ×2
.net ×1
constructor ×1
gcc ×1
implicit ×1
lambda ×1
std-function ×1
templates ×1