1 year ago

#341308

test-img

oolleehh

Why this C++ template method is not compiled?

#include <iostream>

enum class Test { One, Two, Three };

template<Test T> static auto some_func() {
    static_assert(false, "use specification");`
}

template<> auto some_func<Test::One>() { std::cout << "Test One" << std::endl; }

template<> auto some_func<Test::Two>() { std::cout << "Test Two" << std::endl; }

int main() {
    some_func<Test::One>();
}

In latest update VS2022 (17.1.2), this code stopped compile, but in previous versions build was ok.

Source.cpp(6,16): error C2338: use specification.

But if I add some condition to static_assert:

static_assert(std::is_same_v<T, int> && !std::is_same_v<T, int>, "use specification");

It will be build ok.

Why build fails first time?

And why not failing second time? Because condition in second code, will be also False.

c++

templates

c++20

visual-studio-2022

static-assert

0 Answers

Your Answer

Accepted video resources