Giter Club home page Giter Club logo

cpp's Introduction

C++ Tip of The Week


Your weekly dose of modern C++ challenge (Release every Sunday).


Tips

C++26

C++23

C++20

C++17

C++14

C++11

C++98

GNU-extensions

Circle


Disclaimer This repo orignates from https://quantlabfinancial.github.io/cpp_tip_of_the_week

cpp's People

Contributors

andre-nguyen avatar brenoguim avatar daixtrose avatar elbeno avatar eyonland avatar flowingskiff avatar igrekster avatar jbbjarnason avatar jgopel avatar kris-jusiak avatar krzysztof-jusiak avatar ldm5180 avatar liyeming avatar matrackif avatar mirosuaf avatar nevermore1994 avatar paulls20 avatar robertshepherdcpp avatar schaumb avatar schoppenglas avatar superfola avatar web-flow avatar yan-zaretskiy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpp's Issues

std::ostream_joiner in C++20

Hey,
these tips of the week look really interesting and informative, thanks for sharing them.
But I am not sure if it is correct what you write in Tip190. I think std::ostream_joiner did not get added to C++20, or did it?

Regards,
Kilian

Tip 315: why `... + std::size_t{}`?

In 315.md the first proposed solution is

template<class T, class... Ts>
constexpr auto count_compatible = (std::is_layout_compatible_v<T, Ts> + ... + std::size_t{});

but the tests pass without + std::size_t{}.

template<class T, class... Ts>
constexpr auto count_compatible = (std::is_layout_compatible_v<T, Ts> + ...);

I think this is due to C++ being weird (aka have automatic conversions)

constexpr bool b1 = true;
constexpr bool b2 = true;
constexpr auto test = b1 + b2;
static_assert(2 == test);
static_assert(std::is_same_v<decltype(test), const int>);

The introduction of size_t might help for very large type lists, but it probably should go first. Not sure though.

template<class T, class... Ts>
constexpr auto count_compatible = (std::size_t{} + ... + std::is_layout_compatible_v<T, Ts>);

Am I missing something?

Confused about Tip 182

template<class... Ts, class T>
void foo(Ts..., T t) { return t; }
int main() {
  std::cout << foo(1); // prints 1
  std::cout << foo(1, 2, 3, 4); // prints 4
  std::cout << foo(); // compilation error
}

https://godbolt.org/z/boxjzq

The function foo's return type is void, so std::cout will not get a value from foo.

In the compiler explore, the foo(1, 2, 3, 4) also gets an error.

<source>:8:16: error: no matching function for call to 'foo'
  std::cout << foo(1, 2, 3, 4); // prints 4
               ^~~
<source>:4:3: note: candidate function [with Ts = <>, T = int] not viable: requires single argument 't', but 4 arguments were provided
T foo(Ts..., T t) { return t; }
  ^

Is this what the tip want?

I rewrite the function foo like below.

template <class ... Ts, class T>
T foo(T t, Ts ...) { return t; }

The second call will print 1 instead of 4.

So I also want to know how to write a function that looks like the one in tip 182 and gets no error when prints 4.

Maybe I will use function like below:

template <class T>
T foo(T t) { return t;}

template <class ... Ts, class T>
T foo(T t, Ts... args) { return foo(args ...); }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.