0

As you know, you can't write such code in C++:

struct Test
{
    Test _this;
};

But this is quite possible

struct Test
{
    Test* _this;
};

That's when I got this error while writing the following code

error: 'std::pair<_T1, _T2>::second' has incomplete type
       _T2 second
struct Test
{
    std::unordered_map<int, Test> dictionary;
};

I thought that the msvc implementation of std::pair uses pointers for storage.

It's true? If not, what is the difference between the implementations and what is the reason for this?

https://godbolt.org/z/vafxjW5vd

  • 1
    The linked question explains the arkane prohibition about incomplete types used as template parameters (the same thing applies to `unordered_map` as to `vector`), so this boils down to implementation-defined behavior. My opinion is that this is a defect in the standard. – Sam Varshavchik Apr 22 '21 at 12:18
  • I expect the error message to be a bit longer; does it mention `_T2 = Test` or a similar Test-related type? – MSalters Apr 22 '21 at 13:13
  • @MSalters No. Only such a chain of errors https://gist.github.com/uselessgoddess/c2df049ce55184abfb7981641dd25ca3 – uselessgoddess Apr 22 '21 at 13:28
  • @dodickgod: The actual `pair` is in the very first line. You might note that compared to `std::unordered_map`, that `pair` adds a `const` to `int`. – MSalters Apr 22 '21 at 13:32

0 Answers0