模板类方法的部分专业化或实例化 [英] Partial specialization or instantiation of template class method

查看:46
本文介绍了模板类方法的部分专业化或实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有几个模板参数的模板结构

I have template struct with several template parameters

template<class Result, class T, class K>
struct MyClass
{
public:
    Result foo()
    {
        return Result{};
    }
};

此结构对所有模板都适用,但Result为空的情况除外.我了解到, Result {} 不能实现为void类型,所以我当前的解决方案是使用像这样的部分专业化:

This struct works fine for all templates except the case when Result is void. I understand, that Result{} cannot be implemented to void type, so my current solution is to use partial specialization like this:

template<class T, class K>
struct MyClass<void, T, K>
{
public:
    void foo()
    {
        return;
    }
};

这允许执行以下操作:

int main()
{
    MyClass<void, double, char> mycl1;
    MyClass<int, double, char> mycl2;

    mycl1.foo();
    mycl2.foo();
}

有没有一种方法可以使 mycl1.foo()在C ++ 14标准中无需部分类专门化的情况下进行编译?我可以使用 if constexr 并键入trait is_void_v 组合,但是我想查找是否有办法:

Is there a way to make mycl1.foo() compile without partial class specialization in C++ 14 standart? I could use if constexr and type trait is_void_v combination, but I want to find if there is a way to:

  • 模板类方法的专业化(部分显式)

  • specialization of template class method partial explicit

模板类方法的实例

推荐答案

虽然不能做

Result foo()
{
    return Result{};
}

如果 Result void ,则可以使用

Result foo()
{
    return Result();
}

在这种情况下的行为是相同的,并且您将获得一个返回值的初始化对象.如果

The behavior in this case is the same and you will get a value initialized object returned. This syntax is allowed when Result is void by [expr.type.conv]\2

如果初始化程序是带括号的单个表达式,则类型转换表达式(在定义性上,如果定义了含义)与相应的强制转换表达式等效.如果类型为cv void且初始值设定项为(),则表达式为不执行初始化的指定类型的prvalue.否则,表达式是指定类型的prvalue,其结果对象使用初始化程序直接初始化.对于形式为T()的表达式,T不得为数组类型.

If the initializer is a parenthesized single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression. If the type is cv void and the initializer is (), the expression is a prvalue of the specified type that performs no initialization. Otherwise, the expression is a prvalue of the specified type whose result object is direct-initialized with the initializer. For an expression of the form T(), T shall not be an array type.

尽管很快就能使用

return Result{};

即使 Result void ,因为C ++ 20已添加到该部分, {} 对于 void也将同样适用. [expr.type.conv] \ 2 现在指出

even if Result is void as C++20 added to that section that {} will work as well for void. [expr.type.conv]\2 now states

如果初始化程序是带括号的单个表达式,则类型转换表达式与相应的强制转换表达式等效.否则,如果类型为cv void且初始化器为()或{}(在包扩展后,如果有的话),则表达式为指定类型的prvalue,不执行初始化.否则,表达式是指定类型的prvalue,其结果对象使用初始化程序直接初始化.如果初始化程序是带括号的可选表达式列表,则指定的类型不得为数组类型.

If the initializer is a parenthesized single expression, the type conversion expression is equivalent to the corresponding cast expression. Otherwise, if the type is cv void and the initializer is () or {} (after pack expansion, if any), the expression is a prvalue of the specified type that performs no initialization. Otherwise, the expression is a prvalue of the specified type whose result object is direct-initialized with the initializer. If the initializer is a parenthesized optional expression-list, the specified type shall not be an array type.

这篇关于模板类方法的部分专业化或实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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