Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] hw03 done #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

[feat] hw03 done #5

wants to merge 2 commits into from

Conversation

yangyueren
Copy link

  • 这次作业的难点在于计算d = d + c + e时, decltype(auto) operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) 没办法对 std::vector<double> + std::variant<int, double>进行处理,因为前者已经被特化为两个variant相加。所以对std::vector<double>std::variant<int, double>顺序进行排列组合,增加了两个重载函数。
  • 为了自动推导返回类型,我将返回类型变为了decltype(auto),但是这里还是有疑问,为了防止滥用自动推导,应该什么时候用decltype(auto),什么时候不用呢?
  • std::visit配合lambda非常好用!

Copy link
Contributor

@archibate archibate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 完成作业基本要求 43/50 分
  • 能够在 PR 描述中用自己的话解释 22/25 分
  • 代码格式规范、能够跨平台 3/5 分
  • 有自己独特的创新点 5/20 分

// 请实现自动匹配容器中具体类型的加法!10 分
return std::visit([&](auto const&x){
return x + b;
}, a);
}

template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::variant<T1, T2> const &a) {
// 请实现自动匹配容器中具体类型的打印!10 分
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10


template <class T1, class T2, class T3>
decltype(auto) operator+(std::variant<T1, T2> const &a, std::vector<T3> const &b) {
// 请实现自动匹配容器中具体类型的加法!10 分
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6

main.cpp Outdated
}

template <class T1, class T2, class T3>
decltype(auto) operator+(std::variant<T1, T2> const &a, std::vector<T3> const &b) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果 variant 里的 T1, T2 不是 vector 呢?

main.cpp Outdated
}

template <class T1, class T2>
std::variant<T1, T2> operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) {
decltype(auto) operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
decltype(auto) operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) {
auto operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) {

decltype(auto) 是用来自动匹配引用类型的,建议用 auto,因为这里只需要匹配值类型。

main.cpp Outdated
// 请实现列表的逐元素加法!10 分
// 例如 {1, 2} + {3, 4} = {4, 6}
using T0 = decltype(T1{} + T2{});
std::vector<T0> res;
for(size_t i=0; i<a.size()&&i<b.size(); i++){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for(size_t i=0; i<a.size()&&i<b.size(); i++){
for(size_t i=0; i<std::min(a.size(),b.size()); i++){

方便编译器识别出不变量从而优化。

@@ -16,19 +20,48 @@ std::ostream &operator<<(std::ostream &os, std::vector<T> const &a) {

// 请修复这个函数的定义:10 分
template <class T1, class T2>
std::vector<T0> operator+(std::vector<T1> const &a, std::vector<T2> const &b) {
decltype(auto) operator+(std::vector<T1> const &a, std::vector<T2> const &b) {
// 请实现列表的逐元素加法!10 分
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9

#include <iostream>
#include <vector>
#include <variant>

#include "cpp_type_name.h"

// 请修复这个函数的定义:10 分
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10

@@ -16,19 +20,48 @@ std::ostream &operator<<(std::ostream &os, std::vector<T> const &a) {

// 请修复这个函数的定义:10 分
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants