What am I doing wrong here? Or is it a clang ++ error?

The following code does not compile on my 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;
}

This is how I compile from the 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 in a preview of Visual Studio 2015.

And it compiles fine from the terminal when I replace the point call:

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

PS: clang ++ version I use: clang ++ --version Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)

+4
source share
1 answer

Replace all instances unsigned intwith std::size_t. The class template is std::arraydeclared as.

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

, , . 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);
+5

All Articles