我正在尝试实施进行不同输入检查的策略。这些策略之一应该是constexpr
检查输入类型的一些条件。
我创建了一个显示意图的最小示例:我希望编译器在处理不正确的输入时返回给定的static_assert
错误消息。 VS似乎忽略了constexpr函数my_policy
(两个重载),这就是为什么我收到错误C2338的原因。在这三种情况下,GCC和Clang都能按预期工作。
您会将其归类为VS错误吗?有办法解决这个问题吗?
最诚挚的问候!
#include <array>
#include <tuple>
template <typename Container, std::size_t MinSize>
constexpr bool is_of_min_size()
{
return (std::tuple_size_v<Container> >= MinSize);
}
template <typename Container>
constexpr void check()
{
static_assert(is_of_min_size<Container, 2>(),
"Container has less than two elements.");
}
template <typename Container>
constexpr void my_policy(Container)
{
check<Container>();
}
template <typename Container>
constexpr void my_policy()
{
check<Container>();
}
template <typename Container>
auto main_call(Container const& t)
{
check<Container>(); //works as expected in all 3 compilers (first error: "Container has less than two elements.", later on: array index out of bounds)
my_policy(t); //returns the expected static_assert failure message in gcc and clange (in gcc as second error), returns 'array index out of bounds' in vs (ignoring static_assert completely)
my_policy<Container>(); //as for my_policy(t)
return std::get<1>(t);
}
int main()
{
using WrongType = std::array<double, 1>;
main_call(WrongType{}) ; //result for this case described in the function main_call
}
编辑:main_call()
应包括这3种情况之一-它们将分别进行测试。
编辑2:将return std::get<1>(t);
替换为return 0;
会提供预期的static_assert
错误消息(这使我更加困惑)