两种模板示例有什么区别?

what is difference of two kinds of template examples?

我打算制作一个 class 'MyVector' 具有 3D 坐标(X、Y、Z)。

我尝试让构造函数具有三种类型的函数参数,每种参数类型都满足std::is_arithmetic。

我做了两个不同的代码。一个运行良好,但另一个运行不正常。

这是我的代码。

main.cpp

#include <iostream>
#include "MyVector.h"

using namespace std;

int main()
{
    MyVector mv1 = MyVector();
    int x = 5;
    double y = 2.0;
    float z = 5.0;
    MyVector mv2 = MyVector(z, y, x);
    //MyVector mv3 = MyVector(&x);


    cout << boolalpha << is_arithmetic<int>::value << endl;
    cout << mv2;

}

MyVector.h

#pragma once

#include <type_traits>
#include <iostream>

class MyVector
{

public:
    MyVector();
    MyVector(const MyVector&);


    //This is What I wanted
    template <typename N1, typename = std::enable_if_t<std::is_arithmetic<N1>::value >
            , typename N2, typename = std::enable_if_t<std::is_arithmetic<N2>::value >
            , typename N3, typename = std::enable_if_t<std::is_arithmetic<N3>::value > >
    MyVector(const N1& x, const N2& y, const N3& z)
    {
        X = x;
        Y = y;
        Z = z;
    }

    //Working
    template <typename N, typename = std::enable_if_t<std::is_arithmetic<N>::value >>
    MyVector(const N& x)
    {
        X = 0;
        Y = 0;
        Z = 0;
    }

    //Not Working
    template <typename N, std::enable_if_t<std::is_arithmetic<N>::value >>
    MyVector(const N& x)
    {
        X = 0;
        Y = 0;
        Z = 0;
    }


private:
    float X;
    float Y;
    float Z;

public:
    friend std::ostream& operator<<(std::ostream&, const MyVector&);

};

我不知道下面两个代码有什么区别

1. template <typename N, typename = std::enable_if_t<std::is_arithmetic<N>::value >>
2. template <typename N, std::enable_if_t<std::is_arithmetic<N>::value >>

这两行代码的运行方式略有不同:

template <typename N, typename = std::enable_if_t<std::is_arithmetic<N>::value
// or with name
template <typename N, typename second = std::enable_if_t<std::is_arithmetic<N>::value

将类型定义为模板(在第一种情况下未命名)并提供默认值(std::enable_if...)。在您的情况下,这归结为 <N=int, second=int>This post 有助于理解在哪里使用模板/类型名。而

2. template <typename N, std::enable_if_t<std::is_arithmetic<N>::value >>

有一个非类型模板参数。这归结为 <N=int, enable_if<...>::type second=?> 两个版本之间的主要区别和开箱即用的一个版本 'working' 的原因是,这一次,未指定此非类型模板假定的值默认。您需要指定它或写类似

的东西
3. template <typename N, std::enable_if_t<std::is_arithmetic<N>::value >* = nullptr>

以下版本是等效的,但提供了一个名称:

4. template <typename N, std::enable_if_t<std::is_arithmetic<N>::value >* second = nullptr>

TLDR

在第二种情况下,您还必须指定 enable-if 守卫的默认值,编译器无法推断出该默认值(或者在调用时同时指定两个模板参数,这是不需要的)。编译器错误提示如果该行(不工作)未与上面的第 (3) 行交换:

/home/juli/te.cc:37:5: note: candidate: ‘template<class N, typename std::enable_if<std::is_arithmetic<_Tp>::value, void>::type <anonymous> > MyVector::MyVector(const N&)’
   37 |     MyVector(const N& x)
      |     ^~~~~~~~
/home/juli/te.cc:37:5: note:   template argument deduction/substitution failed:
/home/juli/te.cc:63:30: note:   couldn’t deduce template parameter ‘<anonymous>’
   63 |     MyVector mv3 = MyVector(x);

根据您使用的标准,研究 c++2a 新引入的 concepts 功能可能会很有趣。