[llvm-bugs] [Bug 46132] New: Error "variable is used but not defined in this translation unit, and cannot be defined..." for a variable which is not ODR-used.

via llvm-bugs llvm-bugs at lists.llvm.org
Fri May 29 08:17:43 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=46132

            Bug ID: 46132
           Summary: Error "variable is used but not defined in this
                    translation unit, and cannot be defined..." for a
                    variable which is not ODR-used.
           Product: clang
           Version: 10.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: officesamurai at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Created attachment 23555
  --> https://bugs.llvm.org/attachment.cgi?id=23555&action=edit
The code in question

I've been trying to implement a type trait that would tell whether
static_cast-ing a derived class to its base will give the same address.
I've come up with the following:

template <typename Base, typename Derived>
struct StaticCastPreservesAddress
{
    static const Derived D;

    static constexpr bool value = decltype(std::bool_constant<static_cast<const
void*>(static_cast<const Base*>(&D)) == static_cast<const
void*>(&D)>{})::value;
};

Here the constant D is deliberately not defined because Derived may not be
default-constructible (and in general because it's runtime overhead).
As I see it, since D is used only inside decltype, it's not ODR-used and its
definition should not be required.

Then I have the following test code:

struct Base1
{
    int i;
};

struct Base2
{
    int j;
};

struct Derived: Base1, Base2
{
    Derived(int) {}
};

static_assert(StaticCastPreservesAddress<Base1, Derived>::value, "");
static_assert(!StaticCastPreservesAddress<Base2, Derived>::value, "");

When put outside of the anonymous namespace, this works with the warning
"instantiation of variable ... required here, but no definition is available
[-Wundefined-var-template]"
(which is not a big deal because I can suppress it).

However, if I put the code into the anonymous namespace, I get a hard error:
"variable 'StaticCastPreservesAddress<..>::D' is used but not defined in this
translation unit, and cannot be defined in any other translation unit because
its type does not have linkage".

Could it be turned into a warning at least?

P.S. I was able to hoodwink the compiler by implementing it as follows:
template <typename T>
struct Wrapper
{
    T obj;
};

template <typename Base, typename Derived>
struct StaticCastPreservesAddress
{
    static const Wrapper<Derived> D;

    static constexpr bool value = decltype(std::bool_constant<static_cast<const
void*>(static_cast<const Base*>(&D.obj)) == static_cast<const
void*>(&D.obj)>{})::value;
};

But I'm afraid that future versions of Clang will become smarter and this trick
will no longer work.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200529/38259ee0/attachment-0001.html>


More information about the llvm-bugs mailing list