mercredi 24 janvier 2018

extern variable resets itself in visual studio

Consider the following example:

// header props.h

#pragma once
#include <vector>
extern std::vector<int> prop;
struct addToVec
{
    addToVec(int a)
    {
        prop.push_back(a);
    }
};

// cpp file props.cpp

#include "props.h"
std::vector<int> prop;
addToVec b(10);

// main.cpp

#include <iostream>
#include "props.h"
addToVec a2(14);
int main(int argc, char** argv)
{
    std::cout << prop.size();
    return 0;
}

The expected out is 2. But the actual output is 1.

This gives the expected result (2) on gcc compiler (not sure of the version). I am using visual studio professional 2013 version 12 update 5. Is this a bug or an expected result?

Aucun commentaire:

Enregistrer un commentaire