考虑这个接口和类......
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) 给出以下示例:我想截断字符串以满足某些长度限制,例如与 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,我想要实现的是:
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) 我使用第三方容器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) 我有以下代码:
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)
为什么会出现这种情况呢?使用用户定义的转换时,是否有一些不能使用的指针算术特殊规则?
我将 int32 类型的张量除以 int32 类型的张量,结果是 float64。我找不到关于为什么会发生这种情况的答案,或者 Tensorflow 如何执行此操作背后是否存在隐含规则。我还没有为任何张量明确定义 dtype,但我已经检查了所有张量,并且在除法之后它们都没有 64 位类型。
我尝试过使用不同的除法公式,例如 tf.divide,都给出相同的结果。
我的代码如下所示:
a_cdf = a / tf.size(a)
Run Code Online (Sandbox Code Playgroud)
具有 tf.int32 类型。
我想要得到的是 float32 形式的结果,这样我就可以编写我的函数而无需显式转换。
我一直在阅读各种来源,字符串文字在程序的整个生命周期中都保留在内存中。在那种情况下,这两个函数之间有什么区别
char *f1() { return "hello"; }
char *f2() {
char str[] = "hello";
return str;
}
Run Code Online (Sandbox Code Playgroud)
虽然f1编译正常,但f2抱怨我正在返回堆栈分配的数据。这里会发生什么?
str指向实际的字符串文字(具有静态持续时间),为什么会出现错误?str,那么原始字符串文字去哪里了?它是否保留在内存中而不引用它?我有这个编译时序列生成类(取自此处,并对其进行了一些修改):
#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
考虑以下代码
#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 进行测试。
// Variadic template class
template<class ... T>
class Bar
{ …Run Code Online (Sandbox Code Playgroud) c++ templates variadic-functions implicit-conversion variadic-templates
有人可以向我解释为什么会发生以下情况吗?
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) 这两个声明有什么区别:
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
c++ ×4
c ×2
c++11 ×2
arrays ×1
c# ×1
casting ×1
gcc ×1
if-statement ×1
json.net ×1
r ×1
scala ×1
static-cast ×1
templates ×1
tensorflow ×1
typechecking ×1