[英]C++ - Could not deduce template argument
I have a sample code which makes use of template programming, it runs fine on linux. 我有一个使用模板编程的示例代码,它在linux上运行良好。 But when I try to bring it to windows with visual studio 12, I got compile error about template argument deduction. 但是,当我尝试将其带到Visual Studio 12的Windows中时,出现了有关模板参数推导的编译错误。 Here is the fraction of code that cause the error: 以下是导致错误的部分代码:
template <int I>
class assign_array
{
public:
template <typename U, unsigned N>
static inline void run(improved_builtin<U, N>& a, const U b[N])
{
// do sth
}
};
template <template <int> class A, int I, int E>
struct loop_iter
{
template <typename U, typename V>
static inline void iter(U& a, V& b)
{
A<I>::run(a, b); // get error here
}
};
template <typename T, unsigned N>
improved_builtin<T, N>::improved_builtin(const T v[N])
{
loop_iter<assign_array, 0, N - 1>::iter(*this, v);
return;
}
The error occurs at A::run(a, b) => assign_array<0>::run(improved_builtin &,const U [N])' : could not deduce template argument for 'const U [N]' from 'const int *' 错误发生在A :: run(a,b)=> Assign_array <0> :: run(improved_builtin&,const U [N])':无法从'const推导出'const U [N]'的模板参数int *'
And I have noticed something strange in the error message which is improved_builtin. 而且我注意到错误消息改良了[builtin]中有些奇怪的东西。 In the assign_array class, the signature of the first argument should be improved_builtin. 在assign_array类中,第一个参数的签名应被改进。 I have no idea why US appears there. 我不知道为什么美国出现在那里。 Anyone has any idea about this error? 有人对此错误有任何想法吗?
When you pass an array in to a function it will decay to a pointer, so you will lose the size that you are trying to deduce a template argument from. 当您将数组传递给函数时,它将衰减为指针,因此您将失去尝试从中推导模板参数的大小。 Passing the array by reference will preserve the type and allow the deduction to take place: 通过引用传递数组将保留类型并允许进行推导:
static inline void run(improved_builtin<U, N>& a, const U (&b)[N])
// take by reference ^
The reason that g++ (and clang) are able to compile your example anyway is that they use your improved_builtin
argument to deduce the type of U
and the value of N
instead of the array type. g ++(和clang)之所以能够编译您的示例,是因为它们使用您的improved_builtin
参数来推导U
的类型和N
的值,而不是数组类型。 For some reason VS2012 doesn't do this and tries to deduce from the array, which isn't valid because it has decayed. 由于某种原因,VS2012不会执行此操作,而是尝试从数组中推断出,这是无效的,因为它已经衰减。 If you didn't have that improved_builtin
argument, your example wouldn't compile at all. 如果没有那个improved_builtin
参数,那么您的示例将根本无法编译。
The solution is as mentioned by TartanLlama and Johny in the comments. 正如TartanLlama和Johny在评论中提到的那样。 But chose TartanLlama's soltution cause it's easier to modify: 但是选择TartanLlama的解决方案是因为它更容易修改:
"Changing improved_builtin and run to take the array by reference instead might work: const T (&v)[N] and const U (&b)[N]" “更改changed_builtin并运行以通过引用获取数组可能会起作用:const T(&v)[N]和const U(&b)[N]”
This is because passing by reference help us to preserve the array size. 这是因为通过引用传递有助于我们保留数组大小。 But I still haven't known why it can compile and run using g++. 但是我仍然不知道为什么它可以使用g ++进行编译和运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.