fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<size_t N>
  5. constexpr size_t sum(const char (&ch)[N]) {
  6. size_t result = 0;
  7. for(size_t i = 0; i < N; ++i) result += ch[i];
  8. return result;
  9. }
  10.  
  11. template<size_t N>
  12. size_t calc(size_t add) { return N + add; }
  13.  
  14. template<size_t N>
  15. size_t test(const char (&ch)[N], int add) { return calc<sum<N>(ch)>(add); }
  16.  
  17. int main() {
  18. cout << calc<sum("00")>(4); // compiles
  19. cout << test("00", 4); // fails
  20. return 0;
  21. }
Compilation error #stdin compilation error #stdout 0s 3460KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'size_t test(const char (&)[N], int) [with unsigned int N = 3u; size_t = unsigned int]':
prog.cpp:19:22:   required from here
prog.cpp:15:68: error: no matching function for call to 'calc(int&)'
 size_t test(const char (&ch)[N], int add) { return calc<sum<N>(ch)>(add); }
                                                                    ^
prog.cpp:12:8: note: candidate: template<unsigned int N> size_t calc(size_t)
 size_t calc(size_t add) { return N + add; }
        ^
prog.cpp:12:8: note:   template argument deduction/substitution failed:
prog.cpp:15:63:   in constexpr expansion of 'sum<3u>((* & ch))'
prog.cpp:15:68: error: 'ch' is not a constant expression
 size_t test(const char (&ch)[N], int add) { return calc<sum<N>(ch)>(add); }
                                                                    ^
prog.cpp:15:68: note: in template argument for type 'unsigned int' 
stdout
Standard output is empty