【问题标题】:Is typename T in call parameter allowed? [duplicate]是否允许调用参数中的类型名 T? [复制]
【发布时间】:2019-09-10 15:19:25
【问题描述】:

抱歉,我无法为这个问题取一个更好的标题。

我为this SO post写了一个代码;看起来像:

#include <iostream>
#include <string>
#include <type_traits>

template < typename T, typename ...Ts> T f(const T &a, Ts&&... args)
{
    return a;
}

template < typename R, typename T, typename... Ts>
typename std::enable_if<!std::is_same<R, T>::value, R>::type
f(typename T, Ts&&... args)
//^^^^^^^^^^ --->HERE
{
    return f<R>(std::forward<Ts>(args)...);
}

int main() {
    std::cout << f<int>('a', std::string{ "string" }, 12); 
    return 0; 
}

你可以看到,我打错了(大概)

f(typename T, Ts&&... args)
//^^^^^^^^^^

但是,当-O3 -std=c++11 时,代码与msvc v19.14 兼容。

另一方面,GCC 和 clang 提供编译器错误:https://godbolt.org/z/HugROb

来自 GCC

error : 'template<class R, class T, class ... Ts> typename std::enable_if<(! std::is_same< <template-parameter-1-1>, <template-parameter-1-2> >::value), R>::type f' conflicts with a previous declaration
typename std::enable_if<!std::is_same<R, T>::value, R>::type f(typename T, Ts&&... args)
^ ~~~~~~~
note : previous declaration 'T f(T, Ts&& ...)'

template < typename T, typename ...Ts> T f(T a, Ts&&... args)
^
error : expected nested - name - specifier before 'T'
typename std::enable_if<!std::is_same<R, T>::value, R>::type f(typename T, Ts&&... args)
^
error : expected '(' before 'T'
error : expected primary - expression before '&&' token
typename std::enable_if<!std::is_same<R, T>::value, R>::type f(typename T, Ts&&... args)
^ ~
warning : variable templates only available with - std = c++14 or -std = gnu++14
typename std::enable_if<!std::is_same<R, T>::value, R>::type f(typename T, Ts&&... args)
^
error : expected ';' before '{' token
{
^

来自clang

<source>:13 : 74 : error : expected a qualified name after 'typename'
typename std::enable_if<!std::is_same<R, T>::value, R>::type f(typename T, Ts&&... args)
^
<source> : 13 : 74 : error : expected a qualified name after 'typename'
<source> : 13 : 74 : error : expected ')'
<source> : 13 : 64 : note : to match this '('
typename std::enable_if<!std::is_same<R, T>::value, R>::type f(typename T, Ts&&... args)
^
<source> : 15 : 34 : error : use of undeclared identifier 'args'; did you mean 'abs' ?
return f<R>(std::forward<Ts>(args)...);
^~~~
abs
/ usr / include / stdlib.h:837 : 12 : note : 'abs' declared here
extern int abs(int __x) __THROW __attribute__((__const__)) __wur;
^

真的可以这样吗?还是msvc编译器的bug?

【问题讨论】:

    标签: c++ c++11 templates syntax language-lawyer


    【解决方案1】:

    解析器指南typename 可以出现only before 一个限定名称(即带有::)。在非限定名称之前永远不需要它,因为这样的名称必须引用一些已知的声明,因此如果它们引用了类型,则已知它们。

    GCC 和 Clang 非常明确地说明了这一点(尽管在尝试从错误的解析中恢复时,它们都会产生不太有用的错误)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 2016-12-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      相关资源
      最近更新 更多