标签: deduction-guide

`std::make_optional` 的重点是什么

std::make_通过引入类模板参数推导(除了make_uniquemake_shared),所有这些都被 C++17 变得多余。

那么重点是std::make_optional什么?至于我可以告诉它的确切同样的事情扣除导游的std::optional

有没有std::make_optional比演绎指南更受欢迎的场景?

c++ c++17 stdoptional ctad deduction-guide

12
推荐指数
1
解决办法
2087
查看次数

如何编写聚合模板别名的推导指南?

使用 C++20,可以为别名模板生成推导准则(请参阅https://en.cppreference.com/w/cpp/language/class_template_argument_deduction 上的“别名模板的推导”部分)。然而,我无法让它们使用聚合初始化语法。在这种情况下,似乎没有生成别名的扣除指南。

看这个例子:

#include <array>

template <size_t N>
using mytype = std::array<int, N>;

// Deduction guideline ???

int main() {
    // mytype error_object = {1, 4, 7}; // ERROR
    mytype<3> object = {1, 4, 7}; // OK, but I have to manually specify the size.
    return object[0];
}
Run Code Online (Sandbox Code Playgroud)

我曾尝试编写演绎指南,但每次都会出现编译器错误。

template <typename T, typename ... U>
mytype(T, U...) -> mytype<1+sizeof...(U)>; // Compiler error
Run Code Online (Sandbox Code Playgroud)

以及我能想到的任何其他准则。

甚至可以自动推导出数组别名的大小吗?

我正在使用 GCC 10.2

c++ aggregate-initialization template-aliases c++20 deduction-guide

11
推荐指数
1
解决办法
217
查看次数

std::array 的推导指南

在 C++ 标准的 C++ 17 和 C++ 20 工作草案中,类模板的推导指南std::array定义如下

template<class T, class... U>
array(T, U...) -> array<T, 1 + sizeof...(U)>;
Run Code Online (Sandbox Code Playgroud)

例如这个声明的结果

std::array a = { 1ll, 2llu };
Run Code Online (Sandbox Code Playgroud)

应该被编译并且变量的推导类型astd::array<long long, 2>.

然而,编译器使用另一个推导指南来检查所有初始值设定项是否具有相同的类型。

这是编译器的错误还是 C++ 17 和 C++20 标准中的推导指南确实发生了变化?

c++ stdarray c++17 c++20 deduction-guide

8
推荐指数
1
解决办法
503
查看次数

我可以在`std`名称空间中添加一个推导指南吗?

假设我要制作一个新的演绎指南,以便进行以下操作?

std::string str;
std::basic_string_view sv = str;
Run Code Online (Sandbox Code Playgroud)

Would that be an Ok customization ?

c++ c++-faq language-lawyer deduction-guide class-template-argument-deduction

7
推荐指数
1
解决办法
121
查看次数

演绎指南是否需要 noexcept 说明符?

出于某些原因,我一直认为演绎指南必须具有noexcept它们所引用的构造函数的相同性。例如:

template<typename T>
struct clazz {
    clazz(const T &) noexcept {}
};

clazz(const char &) noexcept -> clazz<int>;
Run Code Online (Sandbox Code Playgroud)

也就是说,如果构造函数是noexcept并且我也希望它也是 for const char &,我还必须将说明noexcept符添加到演绎指南中。

今天我和 ICC 合作了一下,发现它noexcept在扣除指南上有问题。到现在为止还挺好。我认为这是编译器的错误,仅此而已。
然而,我去研究了标准,找不到任何可以证实我最初假设的点。因此,我对 clang 进行了相同的检查,即使它没有问题,但似乎noexcept在 100% 的情况下,扣除指南上的 。另一方面,构造函数中的那个会影响两者。

所以,我的问题是,它使任何意义还是需要有点传播(如果这根本是有道理的)的noexcept构造函数的-ness还要扣除引导或者是无用的,我可以摆脱所有的noexcept上扣除指南?

c++ templates language-lawyer c++17 deduction-guide

7
推荐指数
1
解决办法
93
查看次数

带维数的多维数组模板

我想制作一个NDArray具有固定尺寸的模板,但可以在每个尺寸上调整大小。

我的问题是如何让它能够根据使用了多少对来推断构造函数中的尺寸{}?构造函数中的元素将用于初始化一些元素。

#include <array>
#include <iostream>

template<typename T, size_t Dimension>
class NDArray
{
    T* buffer = nullptr; //flattened buffer for cache locality
    std::array<size_t, Dimension> dimension;    //keep the current sizes of each dimension
public:
    NDArray(std::initializer_list<T> elements) : dimension{elements.size()}   //for 1D
    {
        std::cout << "Dimension = " << Dimension << '\n';
    }
    NDArray(std::initializer_list<NDArray<T, Dimension-1>> list) //how to make this works???
    {
        std::cout << "Dimension = " << Dimension << '\n';
    }
};

template<typename T, size_t N>
NDArray(const T(&)[N]) …
Run Code Online (Sandbox Code Playgroud)

c++ templates multidimensional-array c++17 deduction-guide

6
推荐指数
1
解决办法
161
查看次数

C++17 Deduction Guide 定义在类内,在类内无效,在类外有用

#include <variant>

struct A { };

struct B { };

struct Test
{
    template<typename Ts>
    struct overloaded : Ts { };

    // 1st Deduction Guide
    template<typename Ts>
    overloaded(Ts)->overloaded<Ts>;

    // 2nd Deduction Guide for class "A"
    overloaded(A)->overloaded<A>;

    void Fun()
    {
        // OK
        overloaded obj1{ A{} };

        // Error: No instance of constructor matches the argument list
        // I think the 1st Deduction Guide is not effective inside class "Test"
        overloaded obj2{ B{} };
    }
};

int main()
{
    // All Deduction …
Run Code Online (Sandbox Code Playgroud)

c++ template-argument-deduction c++17 deduction-guide

5
推荐指数
0
解决办法
130
查看次数

clang vs gcc - 从模板参数派生的结构的 CTAD

考虑以下代码:

template <typename B>
struct D : B { };

D d{[]{ }};
Run Code Online (Sandbox Code Playgroud)
  • gcc 12.x 接受它并推断出结果与预期d一致D</* type of lambda */>

  • clang 14.x 拒绝它并出现以下错误:

<source>:4:3: error: no viable constructor 
              or deduction guide for deduction of template arguments of 'D'
D d{[]{ }};
  ^

<source>:2:8: note: candidate template ignored: 
              could not match 'D<B>' against '(lambda at <source>:4:5)'
struct D : B { };
       ^

<source>:2:8: note: candidate function template not viable: 
              requires 0 arguments, but 1 was provided …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++20 ctad deduction-guide

3
推荐指数
1
解决办法
328
查看次数

提供显式演绎指南是否会禁用隐式演绎指南的生成/形成

我正在阅读C++17 中的演绎指南。假设我们有以下示例:

template<typename T> struct Custom 
{
  
};
template<typename T> struct Person 
{
    Person(Custom<T> const&);
    Person(Custom<T>&&);
};
template<typename T> Person(Custom<T>) -> Person<T>; //explicitly declared deduction guide
Run Code Online (Sandbox Code Playgroud)

我的问题是,显式推导指南的显式声明(如上所示)是否会禁用2 个隐式推导指南(对应于类模板的 2 个成员Person)的形成,如果我们没有指定显式推导,这两个隐式推导指南就会存在指导。我的意思是,假设在上面的例子中,没有显式推导指导,那么就会有2个隐式推导指导(对应于 的两个构造函数Person)。但是当我们添加了显性引导后,我们总共会得到1个推导指导(由我们用户显式声明)还是3个推导指导(包括2个隐性推导指导)。

PS:请注意,问题只是关于 的推导指南Person,而不是关于 是否会形成隐式指南Custom

c++ language-lawyer c++17 ctad deduction-guide

3
推荐指数
1
解决办法
176
查看次数

根据传递给构造函数的参数数量进行推导

这是我正在尝试的东西,但似乎不起作用:我想根据类对象的实例化方式来切换编译时开关。如果只有一个构造函数参数,则LengthOpt应该等于false,否则等于true(我的实现有多个构造函数,其中开关应默认为true.

我尝试创建一个推导指南,但显然如果模板参数没有显示为构造函数参数(这真是令人沮丧),它就不起作用。有谁知道这个问题的不太详细的解决方案?

代码

#include <cstring>
#include <iostream>

const char* str = "some input string";


template <bool LengthOpt>
class some_class
{
public:
    some_class(const char*) {
        std::cout << std::boolalpha << LengthOpt << std::endl;
    }
    some_class(const char*, size_t len) {
        std::cout << std::boolalpha << LengthOpt << std::endl;
    }
};

template <bool LengthOpt> some_class(const char*) -> some_class<false>;
template <bool LengthOpt> some_class(const char*, size_t) -> some_class<true>;

int main()
{
    some_class A(str);
    some_class B(str, strlen(str));
}
Run Code Online (Sandbox Code Playgroud)

错误 …

c++ constructor-overloading c++17 ctad deduction-guide

1
推荐指数
1
解决办法
124
查看次数