Question

I've created Dynamic library project Foo and it has following code in the Foo.h:

#pragma once
#include <memory>
#ifdef MYLIB_EXPORTS
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API __declspec(dllimport)
#endif

class MYLIB_API Foo
{
};

template class MYLIB_API std::tr1::shared_ptr<Foo>;
typedef std::tr1::shared_ptr<Foo> FooPtr;

I use Foo class from my ConsoleApplication1:

#include "stdafx.h"
#include "Foo.h"

template class std::tr1::shared_ptr<Foo>; // (1)

int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
{
    std::tr1::shared_ptr<Foo>(new Foo()); // (2)
    return 0;
}

The code above is compiled without error/warnings. I use Visual Studio 2008 (v90) toolset to compile this. Both projects is compiled with /W4.

Questions:
1. Why (1) doesn't produce any compiler errors/warnings? I've expected here something like C2011 type redefinition. I suspect (1) is ignored.
2. How many instantiations of std::tr1::shared_ptr<Foo> there are? As it is compiled, I've expected that there two instantiation: one in the Foo and another in the consoleapplication1.
3. Which instantiation (if there are many) is used at (2)?


UPDATE1:
I compiled this with Assembly With Source Code (/FAs), and seems that both Foo and ConsoleApplication1 contain implementation of shared_ptr<Foo>. Doesn't this mean that there are two shared_ptr<Foo> explicit instantiation?

Was it helpful?

Solution

(1) isn't a redefinition, it's a forward declaration.

(2) There only needs to be one. I don't know why you expect any more. I supposed it's possible that a given compiler might produce more than one, but why? and it would figure pretty high on their bug list if it did.

(3) See (2).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top