简体   繁体   English

我在这做错了什么?或者这是一个clang ++ bug?

[英]What am I doing wrong here? Or is this a clang++ bug?

The following code fails to compile on my Mac 以下代码无法在我的Mac上编译

#include <iostream>
#include <array>

template <typename T, unsigned int N>
using Vector = std::array<T, N>;

template <typename T, unsigned int N>
T dot(const Vector<T, N> &l, const Vector<T, N> &r) {
    T result{0};
    for (auto i = 0; i < N; ++i) {
        result += l[i] * r[i];
    }
    return result;
}

using Vector3f = Vector<float, 3>;

int main(int argc, const char * argv[]) {
    Vector3f u{1.0f, 2.0f, 3.0f};
    Vector3f v{6.0f, 5.0f, 4.0f};

    std::cout << dot(u, v) << std::endl;

    return 0;
}

Here is how I'm compiling from Terminal: 以下是我从终端编译的方式:

clang++ -std=c++11 -stdlib=libc++ repro.cpp -o repro

Here is the error I get: 这是我得到的错误:

repro.cpp:24:18: error: no matching function for call to 'dot'
    std::cout << dot(u, v) << std::endl;
                 ^~~
repro.cpp:10:3: note: candidate template ignored: substitution failure [with T = float]: deduced non-type template
      argument does not have the same type as the its corresponding template parameter
      ('unsigned long' vs 'unsigned int')
T dot(const Vector<T, N> &l, const Vector<T, N> &r) {
  ^
1 error generated.

The code compiles fine in Visual Studio 2015 Preview. 该代码在Visual Studio 2015 Preview中编译良好。

And it compiles fine from Terminal when I replace the dot call by: 当我通过以下方式替换点呼叫时,它从终端编译得很好:

std::cout << dot<float, 3>(u, v) << std::endl;

PS: clang++ version I'm using: clang++ --version Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn) PS:clang ++我正在使用的版本:clang ++ --version Apple LLVM 6.0版(clang-600.0.57)(基于LLVM 3.5svn)

Replace all instances of unsigned int with std::size_t . std::size_t替换unsigned int所有实例。 The std::array class template is declared as. std::array类模板声明为。

template< class T, std::size_t N > struct array;

During template argument deduction, if the non-type template parameter does not match the corresponding argument, a deduction failure occurs. 在模板参数推导期间,如果非类型模板参数与相应的参数不匹配,则会发生推消失败。 On your system, std::size_t happens to alias long unsigned int Changing the code to the following should work: 在您的系统上, std::size_t碰巧别名为long unsigned int将代码更改为以下内容应该有效:

template <typename T, std::size_t N> // <--
using Vector = std::array<T, N>;

template <typename T, std::size_t N> // <--
T dot(const Vector<T, N> &l, const Vector<T, N> &r);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM