Skip to content

Type trait assertions give the wrong result if used inside a containing struct #60321

@jacobsa

Description

@jacobsa

Here is a tiny program that shows std::is_default_constructible_v is happy with a struct that contains a member initializer:

#include <type_traits>

struct Foo {
  int a = 0;
};

static_assert(std::is_default_constructible_v<Foo>);

This compiles fine with -std=c++17. It also works fine if we embed the struct inside of another one:

#include <type_traits>

struct Bar {
  struct Baz {
    int a = 0;
  };
};

static_assert(std::is_default_constructible_v<Bar::Baz>);

However if we add another type trait use inside of the parent struct, then both type trait assertions fail:

#include <type_traits>

struct Bar {
  struct Baz {
    int a = 0;
  };

  static_assert(std::is_default_constructible_v<Baz>);
};

static_assert(std::is_default_constructible_v<Bar::Baz>);
<source>:8:3: error: static assertion failed due to requirement 'std::is_default_constructible_v<Bar::Baz>'
  static_assert(std::is_default_constructible_v<Baz>);
  ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:11:1: error: static assertion failed due to requirement 'std::is_default_constructible_v<Bar::Baz>'
static_assert(std::is_default_constructible_v<Bar::Baz>);
^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This seems wrong—Baz is already a complete type at the point that the assertion inside Bar is made, right?

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"diverges-from:edgDoes the clang frontend diverge from edg compiler on this issuediverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions