llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29k stars 11.95k forks source link

std::extent returns 0 for an array type of non zero length #33676

Open llvmbot opened 7 years ago

llvmbot commented 7 years ago
Bugzilla Link 34328
Version unspecified
OS Linux
Reporter LLVM Bugzilla Contributor

Extended Description

include

include

template <char ...C> struct ConstantCharsToString { static const char constexpr str[] = { C..., '\0' }; ConstantCharsToString() { printf("%ld\n", std::extent<decltype(str)>::value); } };

int main() { ConstantCharsToString<'h'> foo; }

[razeh@rdx-ws-001 scratch]$ clang++ foo.C -std=c++1z [razeh@rdx-ws-001 scratch]$ ./a.out 0

I was hoping for a 2. clang version 5.0.0 (trunk 298852) (llvm/trunk 298851) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /scratch/razeh/apps/clang-4.0/bin [razeh@rdx-ws-001 scratch]$

llvmbot commented 7 years ago

gcc prints out 2.

llvmbot commented 7 years ago

I think this is related to llvm/llvm-project#32549 .

It looks like clang doesn't seem to be able to deduce the size of constexpr static arrays in a template class.

wheatman commented 1 year ago

This bug still exists in post 17 trunk(a1358225c5b791fa9d901e74fb09606189c0e6af) https://godbolt.org/z/qb9MMEMd3

simplified code which fails compilation instead of at runtime


template <int C>
struct foo
{
    static int constexpr f[] = { C, 0 };
    static_assert(std::extent<decltype(f)>::value);
};

int main()
{
    foo<1> f;
}

compile error

<source>:7:19: error: static assertion failed due to requirement 'std::extent<const int[], 0>::value'
    7 |     static_assert(std::extent<decltype(f)>::value);
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:12:12: note: in instantiation of template class 'foo<1>' requested here
   12 |     foo<1> f;
      |            ^
1 error generated.
Compiler returned: 1

gcc 13.2 compiles fine

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-frontend

Author: None (llvmbot)

| | | | --- | --- | | Bugzilla Link | [34328](https://llvm.org/bz34328) | | Version | unspecified | | OS | Linux | | Reporter | LLVM Bugzilla Contributor | ## Extended Description #include <stdio.h> #include <type_traits> template <char ...C> struct ConstantCharsToString { static const char constexpr str[] = { C..., '\0' }; ConstantCharsToString() { printf("%ld\n", std::extent<decltype(str)>::value); } }; int main() { ConstantCharsToString<'h'> foo; } [razeh@rdx-ws-001 scratch]$ clang++ foo.C -std=c++1z [razeh@rdx-ws-001 scratch]$ ./a.out 0 I was hoping for a 2. clang version 5.0.0 (trunk 298852) (llvm/trunk 298851) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /scratch/razeh/apps/clang-4.0/bin [razeh@rdx-ws-001 scratch]$