C++ 可变参数模板特化(和 static_assert) [英] C++ variadic template specialization (and static_assert)

查看:51
本文介绍了C++ 可变参数模板特化(和 static_assert)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以特化这个模板声明:

Is it possible to specialize this template declaration:

template <class TYPE, class... ARGS> TYPE Foo(ARGS... args) { 
    static_assert(false);
}

我尝试了一些方法,例如:

I tried a few things such as:

template <> int Foo<int>(float args) {
    return 42;
}

...但是当我尝试这样使用它时我总是点击静态断言:

...but I always hit the static assert when I try to use it as such:

auto value = Foo<int>(1.5f);

正确的语法是什么?

推荐答案

作为状态,false 不依赖于模板,所以 static_assert 应该触发.

As state, false is non template dependent, so the static_assert should fire.

您可以在您的情况下使用 = delete :

You may use = delete in your case:

template <class TYPE, class... ARGS> TYPE Foo(ARGS... args) = delete;

template <> int Foo(float) {return 42;}

演示

这篇关于C++ 可变参数模板特化(和 static_assert)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆