小编Luk*_*keG的帖子

Cereal 不支持原始指针

编辑:问题标题是基于对我得到的编译器错误的深刻误解。我(愚蠢地)假设错误是,我试图反序列化为我在函数内部声明的对象。这是完全错误的。我自己没有做足够的调试工作,否则我可以找出问题所在。所以标题非常具有误导性,我改变了它。谢谢 ?????????????????? 为了帮助。


我正在使用谷物在我的引擎中为 3D 模型编写序列化函数,事实证明这非常有效且易于使用。到目前为止,当我测试(反)序列化一个简单的网格时,一切都很好。但是现在我正在尝试反序列化另一个类,但遇到了一个我不明白的问题。

void loadFile(std::string filepath)
{
    DescriptionFile file;

    {
        ifstream stream = ifstream(filepath, std::ifstream::binary);
        PortableBinaryInputArchive archive(stream);
        archive(file);
        stream.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的课程,应该反序列化:

struct DescriptionFile 
{
public:
    DescriptionFile(){}

    map<string, MeshDescription*> meshDescriptions;
    map<string, ModelDescription*> modelDescriptions;

public:
    template<class Archive>
    void serialize(Archive & archive)
    {
        archive(meshDescriptions, modelDescriptions);
    }
};
Run Code Online (Sandbox Code Playgroud)

它给了我编译器错误:谷物不支持序列化原始指针 - 请使用智能指针即使它不是指针。在代码的另一部分中,类似的东西工作得很好。如果有人能帮我解决这个问题,我会很高兴。

c++ serialization cereal

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

如何参考更高种类的参数?

假设您具有以下特征:

trait Foo[A]{
    def foo: A
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个像这样的函数:

def getFoo[A <: Foo[_]](a: A) = a.foo
Run Code Online (Sandbox Code Playgroud)

Scala编译器推断出Any此函数的返回类型。如何_在的签名(或正文)中引用匿名参数getFoo?换句话说,如何取消匿名化参数?

我希望能够使用类似的功能

object ConcreteFoo extends Foo[String] {
  override def foo: String = "hello"
}

val x : String = getFoo(ConcreteFoo)
Run Code Online (Sandbox Code Playgroud)

由于明显的原因,编译失败,因为getFoo它隐式声明为Any

如果使用Scala(对于此问题而言为2.12)无法做到这一点,那么我会对这种限制的理性或技术原因感兴趣。我敢肯定有关于此的文章和现有问题,但是我似乎缺少正确的搜索词。


更新:现有答案可以正确回答我所说的问题,但是我想我对我的实际用例不够准确。对困惑感到抱歉。我想写

def getFoo[A <: Foo[_]] = (a: A) => a.foo

val f = getFoo[ConcreteFoo.type]

//In some other, unrelated place
val x = f(ConcreteFoo)
Run Code Online (Sandbox Code Playgroud)

因为我没有类型的参数A,所以编译器无法推断出参数RA如果

def getFoo[R, …
Run Code Online (Sandbox Code Playgroud)

generics scala

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

返回(大)对象时复制开销?

考虑以下两种简单的Matrix4x4 Identity方法的实现.

1:这个参数采用Matrix4x4参考作为参数,其中直接写入数据.

static void CreateIdentity(Matrix4x4& outMatrix) {
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            outMatrix[i][j] = i == j ? 1 : 0;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

2:这个返回Matrix4x4而不进行任何输入.

static Matrix4x4 CreateIdentity() {
    Matrix4x4 outMatrix;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            outMatrix[i][j] = i == j ? 1 : 0;
        }
    }
    return outMatrix; …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

4
推荐指数
1
解决办法
406
查看次数

为什么涉及std :: enable_if的模板元函数会产生不希望的结果?

我有一个类型特征

template <typename T, typename Enable = void>
struct is_binary_function : std::false_type {};
Run Code Online (Sandbox Code Playgroud)

及其专业化

template <typename function_t>
struct is_binary_function<function_t,
                          std::enable_if_t<
                              !std::is_void_v<typename function_t::result_t> &&
                              !std::is_void_v<typename function_t::parameter1_t> &&
                              !std::is_void_v<typename function_t::parameter2_t> &&
                              function_t::isBinaryCallable
                              , function_t>
        > : std::true_type {};
Run Code Online (Sandbox Code Playgroud)

我试图找出类,具有公共类型定义result_t,parameter1_t以及parameter2_t,以及静态常量isBinaryCallabletrue值.

但是,以下代码不会输出我期望的内容:

struct f {
    using result_t = float;

    using parameter1_t = float;
    using parameter2_t = float;

    static constexpr bool isBinaryCallable = true;
};


int main() {
    using function_t = f;

    std::cout << is_binary_function<f>::value << '\n'; …
Run Code Online (Sandbox Code Playgroud)

c++ templates

4
推荐指数
1
解决办法
105
查看次数

std :: string length()函数如何工作?

我不明白为什么这个循环打印"INFINITE".如果字符串长度为1,怎么会length()-2产生一个大整数?

for(int i=0;i<s.length()-2;i++)
{
    cout<<"INFINITE"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ string loops

0
推荐指数
1
解决办法
232
查看次数

标签 统计

c++ ×4

c++11 ×1

cereal ×1

generics ×1

loops ×1

scala ×1

serialization ×1

string ×1

templates ×1