标签: implicit-conversion

有没有办法让 JSON.Net 使用隐式转换而不是其实际类型来序列化对象?

考虑这个接口和类......

public interface IValueHolder{}

public class ValueHolder<TValue> : IValueHolder {

    public ValueHolder(TValue value) => this.value = value;
    public TValue value { get; }

    public static implicit operator ValueHolder<TValue>(TValue value) => new ValueHolder<TValue>(value);
    public static implicit operator TValue(ValueHolder<TValue> valueHolder) => valueHolder.value;
}

class ValueStorage : Dictionary<string, IValueHolder>{} 
Run Code Online (Sandbox Code Playgroud)

注意到和从的隐式转换TValue。

这段代码的重点是我试图存储键/值对,其中值可以是任何类型,但没有装箱惩罚。这是可能的,因为在设置或从存储中检索值时,我将始终知道类型。(这就是为什么我不只是使用Dictionary<string, object>.)

现在考虑这个代码......

var storage = new ValueStorage();

storage["UltimateQuestion"] = new ValueHolder<string>("What do you get when you multiply six by nine?");
storage["UltimateAnswer"] = new ValueHolder<int>(42);

var jsonSerializerSettings = new …
Run Code Online (Sandbox Code Playgroud)

c# json.net implicit-conversion

5
推荐指数
1
解决办法
810
查看次数

Scala:有没有一种方法可以将类型别名视为与其别名的类型不同?

给出以下示例:我想截断字符串以满足某些长度限制,例如与 SQL 类型的兼容性。

type varchar8 = String

implicit def str2Varchar8(str: String): varchar8 = str.take(8)

val a: varchar8 = "abcdefghi"

// wanted: "abcdefgh", actual result:
a: varchar8 = abcdefghi
Run Code Online (Sandbox Code Playgroud)

编译器似乎没有区分这两种类型。

给定类型别名type A = String,我想要实现的是:

  1. 避免运行时分配(即包装类)
  2. String仅当映射到类型别名时才应用断言/转换的能力A。A即直接使用类型别名作为输入时避免进一步的断言/转换

验证示例:

type NotNullA = A

def method(a: A) = if(a != null)
    _method(a: NotNullA) // explicit typing
  else
    ???

// "a" at runtime is a String but we consider it validated, instead relying on the type system
protected …
Run Code Online (Sandbox Code Playgroud)

casting scala typechecking implicit-conversion

5
推荐指数
1
解决办法
1292
查看次数

有符号和无符号之间的比较。static_cast 是唯一的解决方案吗?

我使用第三方容器int来存储大小。我还使用 stl 容器size_t来存储大小。

我经常在我的代码中必须在同一个循环中使用两者,例如:

// vec is std::vector
// list is the third party container
assert(vec.size() == list.size()); // warning
for(size_t i = 0; i < vec.size(); i++)
{
    vec[i] = list[i]; // warning
}
Run Code Online (Sandbox Code Playgroud)

因此,为了解决这个问题,我必须要么进行函数风格转换,有人告诉我这是变相的 C 风格转换。

// vec is std::vector
// list is the third party container
assert(int(vec.size()) == list.size());
for(size_t i = 0; i < vec.size(); i++)
{
    vec[i] = list[int(i)];
}
Run Code Online (Sandbox Code Playgroud)

或者我可以采用每个人都推荐的更丑陋的解决方案。静态铸造。

// vec is std::vector
// list is the third party …
Run Code Online (Sandbox Code Playgroud)

c++ static-cast implicit-conversion c++11

5
推荐指数
1
解决办法
1064
查看次数

为什么我会收到 Wsign 转换警告?

我有以下代码:

template <typename T>
struct wrapper {
    T t;
    operator T() { return t; }
    T get() { return t; }
};

int main() {
    int a[10];
    int* x = a;
    wrapper<long unsigned int> y{2};
    std::cout << (x + y); // warning
}
Run Code Online (Sandbox Code Playgroud)

当我在 gcc 上编译它(在 7.3.0 和 8.2.0 上测试)时,-Wsign-conversion我收到“警告:从‘long unsigned int’转换为‘long int’可能会改变结果的符号”。如果y有 type long unsigned int,则没有警告。此外,当我明确调用时y.get()也没有警告:

std::cout << (x + y.get()); // this is ok
Run Code Online (Sandbox Code Playgroud)

为什么会出现这种情况呢?使用用户定义的转换时,是否有一些不能使用的指针算术特殊规则?

c++ gcc type-conversion implicit-conversion c++11

5
推荐指数
1
解决办法
2945
查看次数

为什么 Tensorflow 将 int32/int32 转换为 float64 以及如何停止它?

我将 int32 类型的张量除以 int32 类型的张量,结果是 float64。我找不到关于为什么会发生这种情况的答案,或者 Tensorflow 如何执行此操作背后是否存在隐含规则。我还没有为任何张量明确定义 dtype,但我已经检查了所有张量,并且在除法之后它们都没有 64 位类型。

我尝试过使用不同的除法公式,例如 tf.divide,都给出相同的结果。

我的代码如下所示:

a_cdf = a / tf.size(a)
Run Code Online (Sandbox Code Playgroud)

具有 tf.int32 类型。

我想要得到的是 float32 形式的结果,这样我就可以编写我的函数而无需显式转换。

implicit-conversion tensorflow

5
推荐指数
1
解决办法
2170
查看次数

C:所有字符串文字都具有静态存储持续时间吗?

我一直在阅读各种来源,字符串文字在程序的整个生命周期中都保留在内存中。在那种情况下,这两个函数之间有什么区别

char *f1() { return "hello"; }
char *f2() {
   char str[] = "hello";
   return str;
}
Run Code Online (Sandbox Code Playgroud)

虽然f1编译正常,但f2抱怨我正在返回堆栈分配的数据。这里会发生什么?

  • 如果str指向实际的字符串文字(具有静态持续时间),为什么会出现错误?
  • 如果字符串文字被复制到局部变量str,那么原始字符串文字去哪里了?它是否保留在内存中而不引用它?

c arrays initialization string-literals implicit-conversion

5
推荐指数
2
解决办法
491
查看次数

使用模板在 size_t 和 int 之间进行隐式类型转换

我有这个编译时序列生成类(取自此处,并对其进行了一些修改):

#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

5
推荐指数
1
解决办法
165
查看次数

可变参数函数中可变参数模板类的隐式转换

考虑以下代码

#include <functional>
template<class ResultType, class ... Args>
void Foo( std::function<ResultType(Args...)> ) {}

void Dummy(int) {}

int main()
{
     Foo<void, int> ( std::function<void(int)>( Dummy ) ); // OK, no deduction and no conversion
     Foo( std::function<void(int)>( Dummy ) ); // OK, template argument deduction
     Foo<void, int>( Dummy ); // Compile error
}
Run Code Online (Sandbox Code Playgroud)

在第三个中,我知道不能进行模板推导,这就是明确指定模板参数的原因。但是为什么没有从void (*)(int)to的显式转换std::function<void(int)>?

我查找了答案,但这些是关于模棱两可的重载解决方案或模板推论,而不是相关主题。

std::function 的模板参数(签名)不是其类型的一部分吗?

使用 std::function 进行模板类型推导

使用 std::function 进行隐式转换

然后我尝试使用我自己的模板类而不是 std::function 进行测试。

// Variadic template class
template<class ... T>
class Bar
{ …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-functions implicit-conversion variadic-templates

5
推荐指数
1
解决办法
169
查看次数

R: ifelse 把 numeric(0) 变成 NA

有人可以向我解释为什么会发生以下情况吗?

ifelse(TRUE, numeric(0), 1)
> [1] NA
Run Code Online (Sandbox Code Playgroud)

我当然希望数字(0)。我怀疑这是因为 ifelse 是矢量化的,例如以下作品,但我不明白到底发生了什么。

if (TRUE) numeric(0) else 1
#> numeric(0)
Run Code Online (Sandbox Code Playgroud)

if-statement r implicit-conversion

5
推荐指数
1
解决办法
207
查看次数

如何声明函数指针参数

这两个声明有什么区别:

double math_operation(double x, double (*func)(double));
double math_operation(double x, double func(double));
Run Code Online (Sandbox Code Playgroud)

它们似乎都在 GCC 中使用相同的调用:

math_operation(2.0, sqrt);
Run Code Online (Sandbox Code Playgroud)

它只是语法糖还是更多?

c function-pointers implicit-conversion function-declaration

5
推荐指数
1
解决办法
78
查看次数