我有一个数学函数,我希望能够接受双精度数或双精度数数组/向量/容器,并且行为略有不同。
\n\n我正在尝试使用 SFINAE 和类型特征来选择正确的函数。
\n\n这是一个最小的例子:
\n\n#include <iostream>\n#include <vector>\n#include <type_traits>\n\ntemplate <typename T>\nconstexpr bool Iscontainer()\n{\n if constexpr (std::is_class<T>::value && std::is_arithmetic<typename T::value_type>::value) {\n return true;\n }\n return false;\n}\n\n// Function 1 (double):\ntemplate <typename T>\ntypename std::enable_if<std::is_arithmetic<T>::value>::type g(T const & t)\n{\n std::cout << "this is for a double" << t << std::endl;\n}\n\n// Function 2 (vec), version 1:\ntemplate <typename T>\ntypename std::enable_if<IsContainer<T>()>::type g(T const & t)\n{\n std::cout << "this is for a container" << t[0] << std::endl;\n}\n\nint main()\n{\n std::vector<double> v {1, 2};\n std::array<double, 2> a …Run Code Online (Sandbox Code Playgroud)