https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71841

Daniel Krügler <daniel.kruegler at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.kruegler@googlemail.
                   |                            |com

--- Comment #2 from Daniel Krügler <daniel.kruegler at googlemail dot com> ---
Given the fact, that a reinterpret_cast expression cannot appear in constant
expressions, I have severe doubts that Clang would accept the code. I tried to
create a complete test-code from the given snippet as follows:

//---------------------------------
#include <stddef.h>
#include <stdio.h>

struct IItem
{
  virtual void printTree(int level) = 0;    
  virtual void SetRoot(IItem* RootItem) = 0;
};

template<IItem* ...aItem>
class Item : public IItem
{
private:
        static constexpr size_t c_NumberOfChilderen = (sizeof...(aItem) > 0 ?
sizeof...(aItem) : 1);
public:
        IItem* RootItem;
        IItem* ChildItem[c_NumberOfChilderen] = { aItem... };

        Item()
        {
                for (size_t x = 0; x < sizeof...(aItem); x++)
                {
                        ChildItem[x]->SetRoot(this);
                }
        }

        void printTree(int level)
        {
                for (int y = 0; y < level; y++)
                        printf("\t");
                printf("%p\n", this);
                for (size_t x = 0; x < sizeof...(aItem); x++)
                {
                        ChildItem[x]->printTree(level + 1);
                }
        }
        void SetRoot(IItem* RootItem)
        {
                RootItem = RootItem;
        }
};

Item<> Level10;
Item<> Level11;
Item<reinterpret_cast<IItem*>(&Level11), reinterpret_cast<IItem*>(&Level10)>
ItemLevel00;

int main()
{
        ItemLevel00.printTree(0);
        return 0;
}
//---------------------------------

and as expected it is rejected by all Clang versions tested (Between 3.5.0
release and 3.9.0 trunk). Visual Studio 2015 accept the code but it shouldn't,
because (among other reasons, see below) it violates [expr.const] p2:

"A conditional-expression e is a core constant expression unless the evaluation
of e, following the rules of the abstract machine (1.9), would evaluate one of
the following expressions:
[..]
— a reinterpret_cast (5.2.10);
[..]
"

GCC currently accepts reinterpret_cast in constant expressions (see bug 49171),
but that is a different story. 

Another reason why your code is invalid is determined by the constraints of
non-type template parameters. According to [temp.arg.nontype] p1 b1, it is
invalid that a template parameter refers to a subobject, but in your code IItem
clearly is a subobject of Item.

I suggest to declare this issue as invalid.

Reply via email to