2014-06-26 55 views
12

我遇到了將可變參數模板解包爲模板別名的問題。打開模板別名中的參數包

以下代碼可與鏘3.4和GCC 4.8但失敗GCC 4.9:

template <typename T, typename...> 
using front_type = T; 

template <typename... Ts> 
struct foo 
{ 
    using front = front_type<Ts...>; 
}; 

GCC 4.9抱怨:

test.cc:7:37: error: pack expansion argument for non-pack parameter 'T' of alias template 'template<class T, class ...> using front_type = T' 
     using front = front_type<Ts...>; 
            ^
test.cc:1:15: note: declared here 
    template <typename T, typename...> 
      ^

存在一個日提交GCC蝽(#59498),但這應該失敗?下面是從C++ core language issue #1430, "pack expansion into fixed alias template parameter list"一些背景:

Originally, a pack expansion could not expand into a fixed-length template parameter list, but this was changed in N2555. This works fine for most templates, but causes issues with alias templates.

In most cases, an alias template is transparent; when it's used in a template we can just substitute in the dependent template arguments. But this doesn't work if the template-id uses a pack expansion for non-variadic parameters. For example:

template<class T, class U, class V> 
    struct S {}; 

    template<class T, class V> 
    using A = S<T, int, V>; 

    template<class... Ts> 
    void foo(A<Ts...>); 

There is no way to express A<Ts...> in terms of S , so we need to hold onto the A until we have the T s to substitute in, and therefore it needs to be handled in mangling.

Currently, EDG and Clang reject this testcase, complaining about too few template arguments for A. G++ did as well, but I thought that was a bug. However, on the ABI list John Spicer argued that it should be rejected.

+0

你可以在'foo'之外移動'front_type' - 它們似乎沒有關係嗎? – PiotrNycz

+0

是的,好點@PiotrNycz。 (雖然它沒有修復它。) – mavam

+0

我用gcc4.8檢查了這個修改過的代碼 - 看起來很有效。 – PiotrNycz

回答

9

據報道,gcc4.9的Bugzilla:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59498

最少的代碼來重現

template <typename T, typename ...> 
using alias = T; 

template <typename ...T> 
using variadic_alias = alias<T...>; 

using Fail = variadic_alias<int>; 

int main() { } 

從解釋來自海外合作伙伴 - 它是否定的很明顯這是一個真正的bug。 這仍然是在gcc bugzilla和DR 1430(http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1430)中進行的討論 - 現在在上面的問題中進行總結。

+0

它被報告爲一個錯誤,但是看着這個評論,好像是設計。 –

+0

是的,它看起來應該失敗?我編輯了來自[#1430](http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1430) – mavam

+0

@ T.C的上下文的問題。好點,我錯過了。 – PiotrNycz