在同一程序中混合使用C和C++代码时,给出了以下示例(此处略微缩写为相关部分).假设buf.h
包含以下内容:
struct buf {
char* data;
unsigned count;
};
// some declarations of existing C functions for handling buf...
Run Code Online (Sandbox Code Playgroud)
然后建议使用
extern "C" {
#include "buf.h"
}
class mybuf : public buf {
public:
mybuf() : data(0), count(0) { }
// add new methods here (e.g. wrappers for existing C functions)...
};
Run Code Online (Sandbox Code Playgroud)
为了在C++中使用带有附加功能的结构.
但是,这显然会产生以下错误:
error: class `mybuf' does not have any field named `data'
error: class `mybuf' does not have any field named `count'
Run Code Online (Sandbox Code Playgroud)
解释的原因在如何在派生类构造函数中初始化基类成员变量中进行了解释 …
Scala语言规范(关于方差注释的第 4.5 节,第 44 页)说
使用上面的第一点,很容易看出(至少在形式上)
trait Covariant[+A] {
def problematic[B <: A](x : B)
}
Run Code Online (Sandbox Code Playgroud)
产生错误消息
error: covariant type A occurs in contravariant position in type >: Nothing <: A of type B
def problematic[B <: A](x : B)
Run Code Online (Sandbox Code Playgroud)
使用第一点和第二点很容易看出
trait Contravariant[-A] {
def problematic[B >: A](x : B)
}
Run Code Online (Sandbox Code Playgroud)
产生错误消息
error: contravariant type A occurs in covariant position in type >: A <: Any of type B
def problematic[B >: A](x : B)
Run Code Online (Sandbox Code Playgroud)
正如我所提到的,很容易从形式上(即遵循方差注释的规则)看出这些错误发生的原因。然而,我无法举出一个例子来说明这些限制的必要性。相反,很容易举出示例来说明为什么方法参数应该更改方差位置,请参见 …
我知道 deeplearning4j 可以从 Keras ( https://deeplearning4j.org/model-import-keras )导入模型,但我对相反的方式感兴趣。所以,
这可以直接或通过某种方式转换使用 ModelSerializer https://deeplearning4j.org/modelpersistence存储的模型)?特别是,我对在 keras.js ( https://github.com/transcranial/keras-js )中使用训练有素的模型很感兴趣。