<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Error "variable is used but not defined in this translation unit, and cannot be defined..." for a variable which is not ODR-used."
href="https://bugs.llvm.org/show_bug.cgi?id=46132">46132</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Error "variable is used but not defined in this translation unit, and cannot be defined..." for a variable which is not ODR-used.
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>10.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>officesamurai@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=23555" name="attach_23555" title="The code in question">attachment 23555</a> <a href="attachment.cgi?id=23555&action=edit" title="The code in question">[details]</a></span>
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.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>