简体   繁体   English

MSVC中的模板模板参数错误,但不包括Clang。 为什么?

[英]Template template parameter errors in MSVC, but not Clang. Why?

I have written this code to help me sort indices that refer to a collection, according to some predicate: 根据一些谓词,我已经编写了这段代码来帮助我对引用集合的索引进行排序:

#include <algorithm>
#include <functional>
#include <vector>

template<template<class> class Pred = std::less>
struct element_is_pred
{
    template<class C>
    struct type : private Pred<typename C::value_type>
    {
        typedef Pred<typename C::value_type> Base;
        C const *c;
        type(C const &c, Base const &pred = Base())
            : Base(pred), c(&c) { }
        bool operator()(
            typename C::size_type const i,
            typename C::size_type const j) const
        { return this->Base::operator()((*c)[i], (*c)[j]); }
    };
};

template<template<class> class P, class C>
static element_is_pred<P>::template type<C const> element_is(
    C const &c,
    P<typename C::value_type> const &pred = P<typename C::value_type>())
{
    return typename element_is_pred<P>::template type<C const>(c, pred);
}

and I'm using it like this: 我正在这样使用它:

int main()
{
    std::vector<size_t> temp;
    std::vector<size_t> indices;
    indices.push_back(0);
    std::stable_sort(
        indices.begin(),
        indices.end(),
        element_is<std::less>(temp));
}

and when I compile it with Clang 3.2: 当我用Clang 3.2编译时:

clang++ -fsyntax-only Test.cpp

it compiles fine. 它编译良好。

But when I try to compile it with Visual C++ 2013, I get tons of errors, like: 但是,当我尝试使用Visual C ++ 2013进行编译时,会出现很多错误,例如:

test.cpp(23) : warning C4346: 'element_is_pred<Pred>::type<const C>' : dependent name is not a type
        prefix with 'typename' to indicate a type
test.cpp(23) : error C2146: syntax error : missing ';' before identifier 'element_is'
test.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Which compiler is correct? 哪个编译器正确?
What is the correct way to write the code to do this? 编写代码的正确方法是什么?

GCC gives the following error: GCC出现以下错误:

error: need 'typename' before 'element_is_pred<Pred>::type<const C>' because 'element_is_pred<Pred>' is a dependent scope

Following that advice, I can get the program to build on GCC by prepending typename : 根据该建议,我可以通过在typename前面加上typename来获取要在GCC上构建的程序:

static typename element_is_pred<P>::template type<C const> element_is(
       ^^^^^^^^

Clang allows the modified version as well. Clang也允许修改后的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用clang编译MSVC std lib。 在类范围内显式模板函数专门化 - Compiling MSVC std lib with clang. Explicit template function specialization at class scope Consexpr function 对模板参数 object 的评估(MSVC 与 clang/gcc) - Constexpr function evaluation on a template parameter object (MSVC vs clang/gcc) 在clang vs gcc和msvc中按方法指针的模板 - Template by method pointer in clang vs gcc and msvc GCC &amp; Clang vs MSVC Bug,同时在函数模板的相同参数子句中扩展模板参数包 - GCC & Clang vs MSVC Bug while expanding template parameter pack in the same parameter clause for function templates Clang vs MSVC:模板函数原型的处理 - Clang vs MSVC: Treatment of template function prototypes GCC vs Clang & MSVC 使用非类型模板参数时出现错误 - GCC vs Clang & MSVC Bug while using non type template parameter 当 MSVC 和 Clang 不允许时,为什么 GCC 允许在此函数模板中推导出返回类型? - Why does GCC allow a deduced return type in this function template when MSVC and Clang don't? 为什么 MSVC 和 Clang 为相同的 function 模板调用产生不同的输出? - Why do MSVC and Clang produce different outputs for the same function template call? MSVC2010带来许多提升模板错误 - many boost template errors with MSVC2010 在MSVC中使用数据成员指针作为模板参数 - Using data member pointer as template parameter in MSVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM