mercredi 10 août 2022

Explicit instantiation unexpected behavior - instantiate using struct with the same name, but other than original

Let's consider the following code:

output_data.hpp

#pragma once

#include <functional>
#include <cstddef>

namespace example {
    
using Callback = std::function<void(int)>;
struct OutputData {

    void * data = nullptr;
    std::size_t size = 5;
    Callback callback = nullptr;
};

}  // namespace example

buffer.hpp

#pragma once
#include <vector>

namespace example {

template <typename T>
struct Buffer {
    Buffer(std::size_t size);

    std::vector<T> buffer_;
};

} // namespace example

buffer.cpp

#include "buffer.hpp"
#include "output_data.hpp"

namespace example {

template <typename T>
Buffer<T>::Buffer(std::size_t size) : buffer_(size) 
{
}

template struct Buffer<OutputData>;

} // namespace example

main.cpp

#include <iostream>
#include <memory>
#include <functional>
#include "buffer.hpp"

namespace example {

using Callback = std::function<void(int)>;

struct OutputData {
    Callback callback = nullptr;
};

}

int main() {
    std::shared_ptr<int> ptr = std::make_shared<int>(5);

    example::Buffer<example::OutputData> buf(3);

    example::Callback simple_callback = [=](int a) {
        std::cout << a << *ptr << "\n";
    };

    buf.buffer_[1].callback = simple_callback;
}

[Godbolt][https://ift.tt/TkEtF7r]

The compilation ends without errors, while the execution ends with Segmentation Fault. Segfault is caused by the fact that in main.cpp I used a different struct than the one in buffer.cpp during explicit instantiation. Why was the code compiled without errors? Is it possible to detect such programming mistakes during compilation? Is there any warning flag for detecting such cases ?

Aucun commentaire:

Enregistrer un commentaire