How to use a templated function to catch the incorrect parameter in C++

1 Answer

0 votes
#include <iostream>

struct Example {
    void Foo(int value) {
        std::cout << "Foo(int value) = " << value << std::endl;
    }

    template <typename T>
    void Foo(T value) = delete;
};

int main() {
    Example e;
    
    int a = 18493;
    e.Foo(a);
    
    unsigned int b = 18493;
    e.Foo(b);
}


 
/*
run:

main.cpp:19:10: error: use of deleted function 'void Example::Foo(T) [with T = unsigned int]'
   19 |     e.Foo(b);
      |     ~~~~~^~~
 
*/

 



answered Jan 20, 2025 by avibootz
...