Skip to main content
added 4 characters in body
Source Link
wohlstad
  • 37.2k
  • 19
  • 80
  • 117

As far as I know, the type of N in T[N] is std::size_t. I'm using C++17, in case it matters.

template<class T, T N>
void check(char const (&)[N]) 
{ std::cout << std::is_same_v<std::size_t, T> << '\n'; }

int main()
{
    char c[5];
    check(c);
    return 0;
}

This prints 1. However, this fails to compile:

template<std::size_t N>
void check(char const (&)[N]) { std::cout << "std::size_t"; }

template<int N>
void check(char const (&)[N]) { std::cout << "int"; }

int main()
{
    char c[5];
    check(c);
    return 0;
}
<source>: In function 'int main()':
<source>:17:12: error: call of overloaded 'check(char [5])' is ambiguous
     check(c);
            ^
<source>:9:6: note: candidate: 'void check(const char (&)[N]) [with long unsigned int N = 5]'
 void check(char const (&)[N]) { std::cout << "std::size_t"; }
      ^~~~~
<source>:12:6: note: candidate: 'void check(const char (&)[N]) [with int N = 5]'
 void check(char const (&)[N]) { std::cout << "int"; }
      ^~~~~
<source>: In function 'int main()':
<source>:17:12: error: call of overloaded 'check(char [5])' is ambiguous
     check(c);
            ^
<source>:9:6: note: candidate: 'void check(const char (&)[N]) [with long unsigned int N = 5]'
 void check(char const (&)[N]) { std::cout << "std::size_t"; }
      ^~~~~
<source>:12:6: note: candidate: 'void check(const char (&)[N]) [with int N = 5]'
 void check(char const (&)[N]) { std::cout << "int"; }
      ^~~~~

If N is std::size_t, isn't the first template overload the best match? I don't understand where the ambiguity is coming from.

As far as I know, the type of N in T[N] is std::size_t. I'm using C++17, in case it matters.

template<class T, T N>
void check(char const (&)[N]) 
{ std::cout << std::is_same_v<std::size_t, T> << '\n'; }

int main()
{
    char c[5];
    check(c);
    return 0;
}

This prints 1. However, this fails to compile:

template<std::size_t N>
void check(char const (&)[N]) { std::cout << "std::size_t"; }

template<int N>
void check(char const (&)[N]) { std::cout << "int"; }

int main()
{
    char c[5];
    check(c);
    return 0;
}
<source>: In function 'int main()':
<source>:17:12: error: call of overloaded 'check(char [5])' is ambiguous
     check(c);
            ^
<source>:9:6: note: candidate: 'void check(const char (&)[N]) [with long unsigned int N = 5]'
 void check(char const (&)[N]) { std::cout << "std::size_t"; }
      ^~~~~
<source>:12:6: note: candidate: 'void check(const char (&)[N]) [with int N = 5]'
 void check(char const (&)[N]) { std::cout << "int"; }
      ^~~~~

If N is std::size_t, isn't the first template overload the best match? I don't understand where the ambiguity is coming from.

As far as I know, the type of N in T[N] is std::size_t. I'm using C++17, in case it matters.

template<class T, T N>
void check(char const (&)[N]) 
{ std::cout << std::is_same_v<std::size_t, T> << '\n'; }

int main()
{
    char c[5];
    check(c);
    return 0;
}

This prints 1. However, this fails to compile:

template<std::size_t N>
void check(char const (&)[N]) { std::cout << "std::size_t"; }

template<int N>
void check(char const (&)[N]) { std::cout << "int"; }

int main()
{
    char c[5];
    check(c);
    return 0;
}
<source>: In function 'int main()':
<source>:17:12: error: call of overloaded 'check(char [5])' is ambiguous
     check(c);
            ^
<source>:9:6: note: candidate: 'void check(const char (&)[N]) [with long unsigned int N = 5]'
 void check(char const (&)[N]) { std::cout << "std::size_t"; }
      ^~~~~
<source>:12:6: note: candidate: 'void check(const char (&)[N]) [with int N = 5]'
 void check(char const (&)[N]) { std::cout << "int"; }
      ^~~~~

If N is std::size_t, isn't the first template overload the best match? I don't understand where the ambiguity is coming from.

edited tags
Link
Adrian McCarthy
  • 50k
  • 16
  • 136
  • 195
Source Link
ABu
  • 12.6k
  • 8
  • 62
  • 126

Type of the array extension in C++

As far as I know, the type of N in T[N] is std::size_t. I'm using C++17, in case it matters.

template<class T, T N>
void check(char const (&)[N]) 
{ std::cout << std::is_same_v<std::size_t, T> << '\n'; }

int main()
{
    char c[5];
    check(c);
    return 0;
}

This prints 1. However, this fails to compile:

template<std::size_t N>
void check(char const (&)[N]) { std::cout << "std::size_t"; }

template<int N>
void check(char const (&)[N]) { std::cout << "int"; }

int main()
{
    char c[5];
    check(c);
    return 0;
}
<source>: In function 'int main()':
<source>:17:12: error: call of overloaded 'check(char [5])' is ambiguous
     check(c);
            ^
<source>:9:6: note: candidate: 'void check(const char (&)[N]) [with long unsigned int N = 5]'
 void check(char const (&)[N]) { std::cout << "std::size_t"; }
      ^~~~~
<source>:12:6: note: candidate: 'void check(const char (&)[N]) [with int N = 5]'
 void check(char const (&)[N]) { std::cout << "int"; }
      ^~~~~

If N is std::size_t, isn't the first template overload the best match? I don't understand where the ambiguity is coming from.