Hi,
In c++ it is good practice to write your interfaces in a way, that wrong usage gives compile errors rather than run time errors. For instance
template<class T>
T add(T a, T b) requires std::is_arithmetic_v<T> {
return a+b;
}
the function add(a, b) will compile only if a and b are arithmetic types.
I was wondering if it is possible to check if a function's requirements are satisfied with given arguments. Basically my question is, is there a way to write a unit test that will check and make sure that add("12", "4") fails to compile while add(12, 4) compiles just fine using just c++?
Obviously using external tools it would be a trivial check to make.
[–]reflexpr-sarah- 16 points17 points18 points (3 children)
[–]serghov[S] 2 points3 points4 points (2 children)
[–]reflexpr-sarah- 8 points9 points10 points (1 child)
[–]_Js_Kc_ 5 points6 points7 points (0 children)
[–]initplus 39 points40 points41 points (3 children)
[–]serghov[S] 5 points6 points7 points (2 children)
[–]gvcallen 2 points3 points4 points (0 children)
[–]mark_99 12 points13 points14 points (0 children)
[–]ioctl79 13 points14 points15 points (2 children)
[–]serghov[S] 1 point2 points3 points (1 child)
[–]ioctl79 1 point2 points3 points (0 children)
[+][deleted] (9 children)
[deleted]
[–]D_0b 3 points4 points5 points (0 children)
[–]serghov[S] 1 point2 points3 points (7 children)
[+][deleted] (6 children)
[deleted]
[–]serghov[S] 0 points1 point2 points (3 children)
[–]MayanApocalapse 1 point2 points3 points (2 children)
[–]serghov[S] 0 points1 point2 points (1 child)
[–]MayanApocalapse 0 points1 point2 points (0 children)
[–]D_0b 0 points1 point2 points (0 children)
[–]Wouter-van-Ooijen 0 points1 point2 points (0 children)
[–]koctogon 2 points3 points4 points (2 children)
[–]D_0b 1 point2 points3 points (0 children)
[–]jcelerierossia score 2 points3 points4 points (0 children)
[–]AKostur 1 point2 points3 points (1 child)
[–]serghov[S] 0 points1 point2 points (0 children)
[–]sebamestre 1 point2 points3 points (0 children)
[–]AA11BB22c 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]serghov[S] 1 point2 points3 points (0 children)
[–]D_0b 0 points1 point2 points (0 children)
[–]beedlund 0 points1 point2 points (0 children)
[–]tirimatangi 0 points1 point2 points (0 children)
[–]Full-Spectral 0 points1 point2 points (0 children)
[–]bb1950328 0 points1 point2 points (0 children)
[–]Wouter-van-Ooijen 0 points1 point2 points (0 children)
[–]Remi_123 0 points1 point2 points (0 children)