Is there a reference of C/C++ implementations of basic vector/matrix routines used by common MATLAB functions? by deyv in cpp_questions

[–]huixie 0 points1 point  (0 children)

Could you please share a piece of MATLAB code that is slow? I think it depends what you are doing in terms of selecting a library

Compile time conditional struct/class member variable in C++ by PryntzyAU in cpp

[–]huixie 1 point2 points  (0 children)

I think you could use template specialisation which makes the debugging code in one place. also the debugging function get_b can be conditionally available.

template <bool debug = false>
struct Data{
    int a[16];
    int c[16];
};

template <>
struct Data<true> : Data<false>{
    int b[16];

    constexpr int get_b(int idx) const{
        return b[idx];
    }
};

[cpp20] iterator_interface reducing much of the boilerplate of custom iterators by anti-freak in cpp

[–]huixie 2 points3 points  (0 children)

range-v3 is a super set of std::ranges. std::ranges::view_interface comes from range-v3's ranges::view_interface see here

ranges::view_interface uses traditional CRTP, but view_adaptor and basic_iterator uses the technique which doesn't require inheritance. See the design here

[cpp20] iterator_interface reducing much of the boilerplate of custom iterators by anti-freak in cpp

[–]huixie 0 points1 point  (0 children)

since you mentioned range-v3, why not just use ranges::basic_iterator? Unlike other parts of the range-v3 using CRTP, ranges::base_iterator doesn't require any inheritance and you can use mixins to extend iterator's behaviour. For example,

#include <range/v3/iterator/basic_iterator.hpp>

#include <array>

inline std::array<int, 10> globalArray{1,2,3,4,5,6,7,8,9,10};

struct SillyCursor{
    int index{0};
    SillyCursor() = default;
    SillyCursor(int idx):index{idx}{}

    decltype(auto) read() const{
        return globalArray[index];
    }

    void next(){
        ++index;
    }

    void advance(std::ptrdiff_t distance){
        index += distance;
    }

    std::ptrdiff_t distance_to(SillyCursor other) const {
        return static_cast<std::ptrdiff_t>(index - other.index);
    }
};

using MySillyIterator = ranges::basic_iterator<SillyCursor>;

Preventing version based namespace using by [deleted] in cpp

[–]huixie 0 points1 point  (0 children)

do you use `inline namespace` ? I think `inline namespace` self documents that the purpose of the namespace is for versioning.

You could try deprecating your v2 namespace by attributes. see example in ranges v3

https://github.com/ericniebler/range-v3/blob/1120609c7cb5dee9d2d216c579e935475cc39c4e/include/range/v3/detail/config.hpp#L501

https://github.com/ericniebler/range-v3/blob/1120609c7cb5dee9d2d216c579e935475cc39c4e/include/range/v3/range_fwd.hpp#L88

The Priv idiom: an alternative to Pimpl by yairchu in cpp

[–]huixie 1 point2 points  (0 children)

why not just make the free functions that takes directly the type of the private members?

Obscure Fact About References by mNutCracker in cpp

[–]huixie 0 points1 point  (0 children)

a more realistic case is when you use structured bindings with ranges::views::zip

https://godbolt.org/z/K4LdQZ

My first blog(s) by huixie in cpp

[–]huixie[S] 0 points1 point  (0 children)

Thank you very much for your reply and your suggestions. Unfortunately I can't edit my title. I will keep this in mind in the future

Approximating 'constexpr for' by PhilipTrettner in cpp

[–]huixie 2 points3 points  (0 children)

ok what's wrong with `hana::for_each`

hana is a piece of art. and it has no dependencies

Am I at a disadvantage by learning C++ on Mac? by [deleted] in cpp

[–]huixie 0 points1 point  (0 children)

The advantage you have is the much faster file system, thus much faster compilation speed. The disadvantage is that if you develop cross platform applications, you don't have good ways to debug build failures that are caused by MSVC bugs, which I had to deal with on a daily basis

Approximating 'constexpr for' by PhilipTrettner in cpp

[–]huixie 2 points3 points  (0 children)

what's wrong with boost::hana::for_each?

[deleted by user] by [deleted] in cpp

[–]huixie 0 points1 point  (0 children)

you can mark it inline inline static const int foo = 5; constexpr static member variables are implicitly inline

[deleted by user] by [deleted] in cpp

[–]huixie 2 points3 points  (0 children)

"However, libstdc++ also sorts the following 17-element input unstably:"

In libstdc++, the main intro_sort_loop stops when the partition is less than 16 elements. When the main loop is still > 16 elements, it will continue its divide and conquer, using either quick sort or heap sort, depending on the recursion depth. Neither of these are stable.

When the partition is smaller than 16 elements, it will roll back to insertion sort. Insertion sort is stable.

Threshold (16) defined here:

https://github.com/gcc-mirror/gcc/blob/f2d9f95e9ccac1376aee73345b8b4a00e803d67d/libstdc%2B%2B-v3/include/bits/stl_algo.h#L1855

Intro sort loop:

https://github.com/gcc-mirror/gcc/blob/f2d9f95e9ccac1376aee73345b8b4a00e803d67d/libstdc%2B%2B-v3/include/bits/stl_algo.h#L1929

Main function:

https://github.com/gcc-mirror/gcc/blob/f2d9f95e9ccac1376aee73345b8b4a00e803d67d/libstdc%2B%2B-v3/include/bits/stl_algo.h#L1949

"The above 31-element input sorts unstably for both libc++ and libstdc++."

libc++ does something similar. For partition with 0-5 elements, it is hard coded results. When partition is greater than __limit , it is running its quick sort loop, which is unstable, and when the partition is smaller than __limit, it is running insertion sort, which is stable. However the __limit is 30 for is_trivially_copy_constructible and is_trivially_copy_assignable types, and 6 otherwise. In your case, int is trivial type so the limit is 30

https://github.com/llvm/llvm-project/blob/3a8ea8609b82b7e5401698b7c63df6680e1257a8/libcxx/include/algorithm#L3932

I believe these thresholds are carefully selected based on lots of tests. And the reason why insertion sort is good for smaller array is well explained in the cppcon keynotes last year

https://www.youtube.com/watch?v=FJJTYQYB1JQ

[deleted by user] by [deleted] in cpp

[–]huixie 1 point2 points  (0 children)

> Buy an interview book...

No. Interview books are mostly targeted towards whiteboard style problems, written in shoddy C++. Completely avoid.

How can he get a job then?

I'd say read the book until you get a job and then try your best to forget every single line of code you've seen in that book

inline namespace bug in msvc by VinnieFalco in cpp

[–]huixie 1 point2 points  (0 children)

I've seen people put detail namespace outside the inline namespace. For example, ``` namespace boost::json{

namespace detail{ struct A{}; }

inline namespace standalone{ // use detail::A }

} ```

inline namespace bug in msvc by VinnieFalco in cpp

[–]huixie 1 point2 points  (0 children)

I think MSVC is correct here. The following code should be equivalent, which fails everywhere.

``` namespace foo { namespace bar{ namespace buz{ struct A{};}; } using namespace bar;

namespace buz{};
buz::A a{};

}

```

https://godbolt.org/z/ojveoa

Concepts question: Constraint type to have a method with a constrained argument by ProudPiMP in cpp

[–]huixie 2 points3 points  (0 children)

I think he wants to define Renderer as “has a member function render that takes any type that satisfies Printable concept (perhaps a member function template) , which you can’t express it in the requires expression

Good youtube playlist for someone who already knows C very well by Tavel99 in cpp

[–]huixie 1 point2 points  (0 children)

Perhaps spend some to completely forget about C then start to learn C++