标签: implicit-conversion

根据以下规则,表达式中的操作数是否被提升为更大的类型?

如果数值表达式包含不同数字类型的操作数(常量和变量),则根据以下规则将操作数提升为更大的类型:

  1. 如果操作数的类型byte,sbyte,char,short,ushort,他们得到转换成int键入
  2. 如果其中一个操作数是int,那么所有操作数都将转换为int
  3. 如果表达还包含的类型的操作数uint和int,那么所有的操作数转换为long
  4. 如果其中一个操作数是long,那么所有操作数都将转换为long
  5. 如果表达式包含类型的操作数ulong和long,然后操作数转换为float
  6. 如果其中一个操作数是float,那么所有操作数都将转换为float
  7. 如果其中一个操作数是double,则所有操作数都转换为double

假设数值表达式包含不同类型的操作数,所有操作数首先会转换为单个数字类型,然后运行时才会尝试计算结果吗?例如,如果变量b1和b2是的byte类型,而i1是int类型,将b1和b2 get转换为int计算之前(b1+b2):

int i2=(b1+b2)+i1
Run Code Online (Sandbox Code Playgroud)

c# operators implicit-conversion

6
推荐指数
1
解决办法
333
查看次数

Scala隐式转换问题

我在以下代码中遇到隐式转换问题:

trait A {
  def send(s: String): String = {
    println(s)
    s
  }
}

object X {
  implicit def toB(a: A): B = new B(a)

  class B(a: A) {
    def <<(s: String): String = a send s
  }
}

object Y {
  implicit def toB(a: A): B = new B(a)

  class B(a: A) {
  }
}

object Test extends App {
  import X._
  import Y._
  val a: A = new A {}
  a << "Test"
}
Run Code Online (Sandbox Code Playgroud)

Test中的最后一个语句导致编译错误:

error: value << …
Run Code Online (Sandbox Code Playgroud)

scala implicit-conversion

6
推荐指数
1
解决办法
270
查看次数

为什么不能从接口到类定义隐式转换运算符?

说,我有一个界面

public interface ISomeControl
{
    Control MyControl { get; }
    ...
}
Run Code Online (Sandbox Code Playgroud)

是否可以像这样定义smth:

public static implicit operator Control(ISomeControl ctrl)
{
    return ctrl.MyControl;
}
Run Code Online (Sandbox Code Playgroud)

或者更确切地说,为什么我不能在C#中这样做?

c# interface implicit-conversion

6
推荐指数
1
解决办法
1770
查看次数

使用泛型隐式参数进行隐式转换

我似乎将隐式转换与通用隐式参数结合起来不起作用,如下面的示例2b所示:

object Test {

  case class Foo(i: Int)
  case class Bar(i: Int)
  case class Zip(i: Int)

  object Foo {
    // 1)
    implicit def toBar(foo: Foo)(implicit g: Int => Bar): Bar = g(foo.i)
    // 2)
    implicit def toT[T](foo: Foo)(implicit g: Int => T): T = g(foo.i)
  }

  // 1)
  implicit val b = (i: Int) => Bar(i)
  val bar: Bar = Foo(3)    

  // 2a)
  implicit val z = (i: Int) => Zip(i)
  val zip: Zip = Foo.toT(Foo(3))

  // 2b)
  val zip2: …
Run Code Online (Sandbox Code Playgroud)

scala implicit-conversion

6
推荐指数
1
解决办法
283
查看次数

C++中的隐式转换

给定以下代码,为什么编译器在构造时不解析隐式转换Bar?也就是说,构造Foo就像构造的那样a(应该)然后用于构造Bar?

#include <string>

class ImplicitlyConvertToChar
{
public:
  ImplicitlyConvertToChar(const char* a_char)
    : m_string(a_char)
  { }

  ImplicitlyConvertToChar(const char* a_char, size_t a_end)
    : m_string(a_char)
  {
  }

  template <typename T_String>
  ImplicitlyConvertToChar(T_String const& a_string)
    : m_string(a_string.begin())
  {
  }

  operator char const * () const
  { return m_string; }

  const char* m_string;
};

class Foo
{
public:

  Foo(const ImplicitlyConvertToChar& a_charLike)
    : m_string(a_charLike)
  { }

  const char* m_string;
};

class Bar
{
public:
  Bar(const Foo& a_foo)
    : m_foo(a_foo)
  { …
Run Code Online (Sandbox Code Playgroud)

c++ implicit-conversion

6
推荐指数
1
解决办法
479
查看次数

为什么以下代码编译和执行成功?

我在.Net 3.5,Visual Studio 2012中编译了以下代码.

当数组被分配给我的IReadOnlyCollection时,我期望在该行上出现错误,因为没有从Array定义到我的接口的隐式转换.它编译成功,也不会创建任何运行时错误.

需要注意的注意事项:

  • 没有引用其他IReadonlyCollection.所以它必须使用我的(IReadonlyCollection被添加到.Net4.5并且在早期版本中不存在)
  • 当我将它重命名为IMyCollection时,它不再编译
  • 当我更改名称空间时,它不再编译.

File1.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Collections.Generic
{
    public interface IReadOnlyCollection<T> : IEnumerable<T>, IEnumerable
    {
        int Count
        {
            get;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

File2.cs:

using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Test
    {
        public Test()
        { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test[] foo = { new Test(), new Test(), new Test() };


            IReadOnlyCollection<Test> bar = foo;

            int count = bar.Count;
        } …
Run Code Online (Sandbox Code Playgroud)

c# arrays .net-3.5 implicit-conversion

6
推荐指数
1
解决办法
142
查看次数

在基于策略的类中保留构造的隐含性

考虑一个基于策略的智能指针类Ptr,只有一个策略可以防止在NULL状态下解除引用它(不知何故).让我们考虑两种这样的政策:

  • NotNull
  • NoChecking

由于NotNull政策限制较多,我们希望允许隐式转换Ptr< T, NoChecking >到Ptr< T, NotNull >,但不能在相反的方向.那一个必须明确安全.请看一下以下实现:

#include <iostream>
#include <type_traits>
#include <typeinfo>

struct NoChecking;
struct NotNull;

struct NoChecking{
  NoChecking()                    = default;
  NoChecking( const NoChecking&)  = default;

  explicit NoChecking( const NotNull& )
  { std::cout << "explicit conversion constructor of NoChecking" << std::endl; }

protected:
  ~NoChecking() {} //defaulting the destructor in GCC 4.8.1 makes it public somehow :o
};

struct NotNull{
  NotNull()                 = default;
  NotNull( const NotNull&)  = default;

  NotNull( const NoChecking& …
Run Code Online (Sandbox Code Playgroud)

c++ constructor policy-based-design implicit-conversion c++11

6
推荐指数
1
解决办法
342
查看次数

从int到shared_ptr的隐式转换

请考虑以下代码:

#include <iostream>
#include <memory>

void f(std::shared_ptr<int> sp) {}

template <typename FuncType, typename PtrType>
auto call_f(FuncType f, PtrType p) -> decltype(f(p))
{
    return f(p);
}

int main()
{
    f(0); // doesn't work for any other int != 0, thanks @Rupesh
    // call_f(f, 0); // error, cannot convert int to shared_ptr
}
Run Code Online (Sandbox Code Playgroud)

在第一行中main(),整数0转换为a std::shared_ptr<int>,调用f(0)成功,没有任何问题.但是,使用模板调用函数会使事情变得不同.第二行将不再编译,错误是

error: could not convert 'p' from 'int' to 'std::shared_ptr<int>'
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 为什么第一次通话成功而第二次通话没有?这里有什么我想念的吗?
  2. 我不也明白如何从转换int到std::shared_ptr被调用执行f(0),因为它看起来像std::shared_ptr只有明确的构造函数. …

c++ type-conversion shared-ptr implicit-conversion c++11

6
推荐指数
1
解决办法
1549
查看次数

按字符串创建类的实例,但将隐式转换考虑在内 - C#Reflection

我处于这样一种情况,我需要创建一个给定Type(作为字符串)的对象实例,以及一个构造函数参数数组.

这就是我实现的目标:

public object Create(string Name, params object[] Args)
{
    return Activator.CreateInstance(Type.GetType(Name), Args);
}
Run Code Online (Sandbox Code Playgroud)

这在大多数情况下都可以正常工作,但是存在问题; 它不会将隐式转换考虑在内.


让我解释一下我的意思,说我们有一个简单的类,其中隐式转换为int定义

public class ImplicitTest
{
    public double Val { get; set; }

    public ImplicitTest(double Val)
    {
        this.Val = Val;
    }

    public static implicit operator int(ImplicitTest d)
    {
        return (int)d.Val;
    }
}
Run Code Online (Sandbox Code Playgroud)

我们有一个使用int作为构造函数参数的类

public class TestClass
{
    public int Val { get; set; }        

    public TestClass(int Val)
    {
        this.Val = Val;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在说我们想要创建一个TestClass实例,我们可以这样做: new TestClass(5).在这种情况下,我们使用构造函数指定的确切参数类型(int).但是我们也可以使用ImplicitTest类创建类的实例,如下所示:new TestClass(new ImplicitTest(5.1)).这是有效的,因为参数是从ImplicitTest隐式转换为int.然而,Activator.CreateInstance()不会这样做.


我们可以使用Create(string Name, params …

.net c# reflection constructor implicit-conversion

6
推荐指数
1
解决办法
629
查看次数

函数定义中使用的命名数组元素

最近我发现这种语法适用于JavaScript(Chrome 53):

function foo([param1]) { // Function argument is declared as array and param1 is used as variable? What is the name of this syntax?
  console.log(param1); 
}

foo(['TestParameter1']); // Case 1 - works. Output: TestParameter1
foo('TestParameter1');   // Case 2 - works??? Why? Output: TestParameter1
foo(123);                // Case 3 - does not work - VM860:1 Uncaught TypeError: undefined is not a function(…)

Result => TestParameter1 // this is the result
Run Code Online (Sandbox Code Playgroud)

我看到param1可以用作引用第一个参数中索引为0的项的变量(声明为数组).

我的问题是:

1)这个语法是如何命名的([param1]部分允许你使用param1作为变量)?

2)为什么"案例2"有效?有自动转换吗?

javascript arrays function implicit-conversion

6
推荐指数
1
解决办法
65
查看次数