1//===------------------------- dynamic_cast_stress.cpp --------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is dual licensed under the MIT and the University of Illinois Open 6// Source Licenses. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include <cassert> 11#include <tuple> 12#include "support/timer.hpp" 13 14template <std::size_t Indx, std::size_t Depth> 15struct C 16 : public virtual C<Indx, Depth-1>, 17 public virtual C<Indx+1, Depth-1> 18{ 19 virtual ~C() {} 20}; 21 22template <std::size_t Indx> 23struct C<Indx, 0> 24{ 25 virtual ~C() {} 26}; 27 28template <std::size_t Indx, std::size_t Depth> 29struct B 30 : public virtual C<Indx, Depth-1>, 31 public virtual C<Indx+1, Depth-1> 32{ 33}; 34 35template <class Indx, std::size_t Depth> 36struct makeB; 37 38template <std::size_t ...Indx, std::size_t Depth> 39struct makeB<std::__tuple_indices<Indx...>, Depth> 40 : public B<Indx, Depth>... 41{ 42}; 43 44template <std::size_t Width, std::size_t Depth> 45struct A 46 : public makeB<typename std::__make_tuple_indices<Width>::type, Depth> 47{ 48}; 49 50void test() 51{ 52 const std::size_t Width = 10; 53 const std::size_t Depth = 5; 54 A<Width, Depth> a; 55 typedef B<Width/2, Depth> Destination; 56// typedef A<Width, Depth> Destination; 57 Destination *b = nullptr; 58 { 59 timer t; 60 b = dynamic_cast<Destination*>((C<Width/2, 0>*)&a); 61 } 62 assert(b != 0); 63} 64 65int main() 66{ 67 test(); 68} 69 70/* 71Timing results I'm seeing (median of 3 microseconds): 72 73 libc++abi gcc's dynamic_cast 74B<Width/2, Depth> -O3 48.334 93.190 libc++abi 93% faster 75B<Width/2, Depth> -Os 58.535 94.103 libc++abi 61% faster 76A<Width, Depth> -O3 11.515 33.134 libc++abi 188% faster 77A<Width, Depth> -Os 12.631 31.553 libc++abi 150% faster 78 79*/ 80