2

鉴于此片段:

template <std::size_t Index, typename T, typename ...Args>
typename type_at<Index, T, Args...>::type
get(T t1, Args... args)
{
        return 
                static_cast<type_at<Index, T, Args...>::type>
                (
                        reinterpret_cast<void*>
                        (
                                value_at<Index, T, Args...>::get(t1, args...)
                        )
                );
}


int main()
{
        int   * a     = new int(10);
        double* b     = new double(3.14);
        std::string c = "But I'm a string :(";

        std::cout<< *get<0>(a, b, &c) <<"\n";
        std::cout<< *get<1>(a, b, &c) <<"\n";
        std::cout<< *get<2>(a, b, &c) <<"\n";
}

这在 GCC 4.8.1 上不起作用,但在 VS2012 中使用 Nov CTP 编译器编译和运行良好(顺便说一下,没有尝试 clang)

哪个编译器是对的?

完整的例子

4

1 回答 1

1

在这种特殊情况下,GCC 更符合标准,因为您在部分代码typename中引用依赖类型时会丢失。type_at<Index, T, Args...>::type

Clang 也在做类似的事情并给出一个很好的错误消息(一如既往):

./test.cc:40:29: error: missing 'typename' prior to dependent type name 'type_at<Index, T, Args...>::type'
                static_cast<type_at<Index, T, Args...>::type>
                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                            typename 
1 error generated.
于 2013-08-12T19:24:07.723 回答