llvm / llvm-project

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

clang disallows having a pointer to subobject as a template argument #61500

Closed brevzin closed 4 weeks ago

brevzin commented 1 year ago

Short example:

template <int const*>
constexpr int f()
{
    return 0;
}

struct X { int i; };
constexpr auto x = X{.i=1};
constexpr int xs = f<&x.i>();

Clang rejects (every version I've tried), saying:

candidate template ignored: invalid explicitly-specified argument for 1st template parameter

But &x.i should meet all the requirements here - it is a pointer to int const and the object in question does have static storage duration (so it should be a permitted result of a constant expression).

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-frontend

shafik commented 1 year ago

I believe this is: https://github.com/llvm/llvm-project/issues/56695

If I modify the code to use a templated struct I receive the same diagnostic: https://godbolt.org/z/n375vaqEM

template <int const*>
constexpr int f() {
    return 0;
}

template <int const&>
constexpr int f2() {
    return 0;
}

template <int const*> struct W{};

struct X { int i; };
constexpr X x = X{.i=1};
constexpr int xs = f<&x.i>();
W<&x.i> w;

struct Z { int i; };
constexpr Z z{};
template<const int&> struct Y { };
constexpr int ys = f2<x.i>();
Y<x.i> y;
llvmbot commented 1 year ago

@llvm/issue-subscribers-c-2b

brevzin commented 1 year ago

@shafik Yep, same issue!

Should be C++20 tho (not 2b)

llvmbot commented 1 year ago

@llvm/issue-subscribers-c-20

cor3ntin commented 4 weeks ago

Fixed in 18