fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <int Size, typename T>
  6. int getArrayLength(T (&s)[Size]) {
  7. return Size;
  8. }
  9.  
  10. template<int Size, typename T>
  11. int getArrayLength(T* s) {
  12. return Size;
  13. }
  14.  
  15. int main() {
  16. int a[100];
  17. int* b = new int[50];
  18.  
  19. cout << getArrayLength(a) << endl; // 100 -- первая функция
  20. cout << getArrayLength<20>(a) << endl; // 20 -- вторая функция
  21. cout << getArrayLength<50>(b) << endl; // 50 -- вторая функция
  22. cout << getArrayLength<100>(a) << endl; // ошибка -- муки выбора
  23.  
  24. return 0;
  25. }
  26.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:22:31: error: call of overloaded ‘getArrayLength(int [100])’ is ambiguous
  cout << getArrayLength<100>(a) << endl; // ошибка -- муки выбора
                               ^
prog.cpp:22:31: note: candidates are:
prog.cpp:6:5: note: int getArrayLength(T (&)[Size]) [with int Size = 100; T = int]
 int getArrayLength(T (&s)[Size]) {
     ^
prog.cpp:11:5: note: int getArrayLength(T*) [with int Size = 100; T = int]
 int getArrayLength(T* s) {
     ^
stdout
Standard output is empty