static_downcast

  • static_castは何をしているか分からないので駄目である
  • そこで実行時にチェックできる場合はチェックするdowncast (実装)
    • VC++7.1のバグのおかげですごく苦労した
  • cv-qualifierは自動なのがミソ

[]#include[] []<pstade/static_downcast.hpp>[]
[]#include[] []<boost/utility/result_of.hpp>[]

[]using[] []namespace[] []pstade[][];[]

[]struct[] []base[]
[]{[]
[]};[]

[]struct[] []derived[][] : [][]base[]
[]{[]
[]derived[][]() { }[]
[]void[] []foo[][]() [][]const[][] { }[]
[]};[]

[]int[] []main[][]()[]
[]{[]
[]derived[] []const[] []D[][];[]
[]base[] []const[][]& [][]crB[][] = [][]D[][];[]

[]boost[][]::[][]result_of[][]<[]
[]op_static_downcast[][]<[][]derived[][]>([][]base[] []const[][]&)[]
[] >::[][]type[] []d[][] = [][]pstade[][]::[][]static_downcast[][]<[][]derived[][]>([][]crB[][]);[]

[]pstade[][]::[][]static_downcast[][]<[][]derived[][]>([][]crB[][]).[][]foo[][]();[]
[]}[]