2012-12-17 43 views
5

考虑下面的代码:两种不同的结果模板模板扣除

#include <iostream> 
#include <vector> 
#include <array> 
#include <type_traits> 

// Version A 
template<typename T> 
void f(const T& x) 
{ 
    std::cout<<"Version A"<<std::endl; 
} 

// Version B 
template<typename... T1, template<typename...> class T> 
void f(const T<T1...>& x) 
{ 
    std::cout<<"Version B"<<std::endl; 
} 

// Version C 
template<typename T1 = double, typename TN = size_t, template<typename, TN...> class T, TN... N> 
void f(const T<T1, N...>& x) 
{ 
    std::cout<<"Version C"<<std::endl; 
} 

// Main 
int main(int argc, char* argv[]) 
{ 
    f(double()); 
    f(std::vector<double>()); 
    f(std::array<double, 3>()); 
    return 0; 
} 

GCC 4.6.2在Windows上给出了:

Version A 
Version B 
Version C 

和GCC 4.7.1在Linux上给:

Version A 
Version B 
Version A 

所以问题是:为什么? 这是一个错误还是一个未定义的行为? 我应该将它发布在GCC错误报告上吗?

+2

不是今天这个刚才问? – Gorpik

+0

删除'= size_t'修复了它。 – Pubby

+0

@Gorpik:它的代码大致相同,但不是相同的问题/原因。 – Vincent

回答

4

它看起来像gcc 4.7.x中的错误(4.7.2有同样的问题)。这里有一个简单的例子:

template<int N> struct S {}; 
template<typename T = int, T N> void f(S<N>) {} 
int main() { S<1> s; f(s); } 

GCC 4.7.2失败:

source.cpp:3:25: error: no matching function for call to 'f(S<1>&)' 
source.cpp:3:25: note: candidate is: 
source.cpp:2:38: note: template<class T, T N> void f(S<N>) 
source.cpp:2:38: note: template argument deduction/substitution failed: 
+1

你能举报这个bug到GCC吗? – Vincent

+2

我在这里报告了这个错误:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55724 – Vincent