C++ Weekly - Ep 431 - CTAD for NTTP

Поделиться
HTML-код
  • Опубликовано: 2 июн 2024
  • ☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
    Upcoming Workshop: Understanding Object Lifetime, C++ On Sea, July 2, 2024
    ► cpponsea.uk/2024/sessions/und...
    Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
    ► ndctechtown.com/workshops/c-b...
    Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024
    ► cppunderthesea.nl/workshops/
    Episode details: github.com/lefticus/cpp_weekl...
    T-SHIRTS AVAILABLE!
    ► The best C++ T-Shirts anywhere! my-store-d16a2f.creator-sprin...
    WANT MORE JASON?
    ► My Training Classes: emptycrate.com/training.html
    ► Follow me on twitter: / lefticus
    SUPPORT THE CHANNEL
    ► Patreon: / lefticus
    ► Github Sponsors: github.com/sponsors/lefticus
    ► Paypal Donation: www.paypal.com/donate/?hosted...
    GET INVOLVED
    ► Video Idea List: github.com/lefticus/cpp_weekl...
    JASON'S BOOKS
    ► C++23 Best Practices
    Leanpub Ebook: leanpub.com/cpp23_best_practi...
    ► C++ Best Practices
    Amazon Paperback: amzn.to/3wpAU3Z
    Leanpub Ebook: leanpub.com/cppbestpractices
    JASON'S PUZZLE BOOKS
    ► Object Lifetime Puzzlers Book 1
    Amazon Paperback: amzn.to/3g6Ervj
    Leanpub Ebook: leanpub.com/objectlifetimepuz...
    ► Object Lifetime Puzzlers Book 2
    Amazon Paperback: amzn.to/3whdUDU
    Leanpub Ebook: leanpub.com/objectlifetimepuz...
    ► Object Lifetime Puzzlers Book 3
    Leanpub Ebook: leanpub.com/objectlifetimepuz...
    ► Copy and Reference Puzzlers Book 1
    Amazon Paperback: amzn.to/3g7ZVb9
    Leanpub Ebook: leanpub.com/copyandreferencep...
    ► Copy and Reference Puzzlers Book 2
    Amazon Paperback: amzn.to/3X1LOIx
    Leanpub Ebook: leanpub.com/copyandreferencep...
    ► Copy and Reference Puzzlers Book 3
    Leanpub Ebook: leanpub.com/copyandreferencep...
    ► OpCode Puzzlers Book 1
    Amazon Paperback: amzn.to/3KCNJg6
    Leanpub Ebook: leanpub.com/opcodepuzzlers_book1
    RECOMMENDED BOOKS
    ► Bjarne Stroustrup's A Tour of C++ (now with C++20/23!): amzn.to/3X4Wypr
    AWESOME PROJECTS
    ► The C++ Starter Project - Gets you started with Best Practices Quickly - github.com/cpp-best-practices...
    ► C++ Best Practices Forkable Coding Standards - github.com/cpp-best-practices...
    O'Reilly VIDEOS
    ► Inheritance and Polymorphism in C++ - www.oreilly.com/library/view/...
    ► Learning C++ Best Practices - www.oreilly.com/library/view/...
  • НаукаНаука

Комментарии • 45

  • @BernieBear42
    @BernieBear42 Месяц назад +8

    Thanks for explaining all these concepts. I couldn't even find something related to NTTP when I did a quick Internet search. I also loved seeing the Demangler website. I didn't know this existed!

  • @trapsaregay8141
    @trapsaregay8141 Месяц назад +13

    C++ committee on its way to design the most powerful compile-time metaprogramming facilities known to man(they saved typing 6 extra characters)

  • @ohwow2074
    @ohwow2074 Месяц назад +2

    Nah I like this feature fr. I can find specific cases where this can be super helpful.

  • @mogenshansen7210
    @mogenshansen7210 Месяц назад +1

    Thanks for the information and example
    It seems to compile with gcc and MSVC, but not with clang

  • @nextlifeonearth
    @nextlifeonearth Месяц назад +2

    Reminds me of this gcc/clang extension with template string literal operator overloads. If I remember correctly it looks like this:
    template
    auto operator""_foo();
    sml uses it for the state notation, for instance.
    To invoke it, it's just "mystring"_foo or the like.
    Maybe this can be expressed some other way in native c++ without the extension (which does work in any compiler worth using. MSVC is the only odd one out)

  • @Caellyan
    @Caellyan Месяц назад +1

    Having to specify template class types is a pain when you want to support std::vector with custom allocators. This becomes very trivial to do right now...

  • @Omnifarious0
    @Omnifarious0 Месяц назад +2

    I wonder how the compiler and/or linker will handle a symbol name that's, say, a couple of megabytes long.

    • @cppweekly
      @cppweekly  17 дней назад

      MSVC might truncate it. Or at least it used to...

  • @michaelkovaliov8877
    @michaelkovaliov8877 26 дней назад +1

    sorry to be nit-picky but the second vector you defined is a vector of double, not float. for float you would need to define `1.f, 2.f` etc

    • @cppweekly
      @cppweekly  17 дней назад +1

      Ok a bit of a miss-speak. In my head it was meant to be "a vector of floating-point values" (Yes I'm aware that floating point values are defaulted double in C and C++)

  • @rudolflovrencic
    @rudolflovrencic Месяц назад +1

    Is there a reason to use that instead of consteval?
    templateauto get_value() { return x; }
    VS
    consteval auto get_value(int x) { return x; }
    Is there a difference between those, besides syntax of the call?

    • @cppweekly
      @cppweekly  17 дней назад

      I believe you can make either work for most contexts. It's a question of ergonomics, largely. Which do you prefer based on what you're trying to do at the moment.

  • @bjorn6437
    @bjorn6437 Месяц назад +2

    That's some crazy shit for a c# developer 😅 I don't have an idea for what I could use this ...

    • @cppweekly
      @cppweekly  17 дней назад

      Just think "how can I make more work happen at compile time" and this will eventually come up. Work done at compile time doesn't have to happen at runtime, and also it can influence the optimizer to do even *more* work at compile time.

  • @ysakhno
    @ysakhno Месяц назад +1

    What I do not understand is this. What is the benefit of passing arrays as template parameters as opposed to passing them as just "normal" parameters? Also: does this work with other types (strings, vectors, maps, etc.)?

    • @jonawhite17
      @jonawhite17 Месяц назад +2

      Template parameters are guaranteed to be resolved at compile time, whereas normal parameters are usually dealt with at runtime.

    • @sledgex9
      @sledgex9 Месяц назад

      Because an std::array is a template itself. eg you can have std::array and std::array which are different types. So if you want to pass them as "normal" parameters you'll have to explicitly write out their template arguments aka std::array and make multiple overloads of the function if you want to take in different kinds of std::array. That's a maintenance nightmare.

    • @ysakhno
      @ysakhno Месяц назад

      @@sledgex9 You are talking rubbish, because you can definitely put the template parameters of the array as a template parameters for your function:
      template T get_value(std::array a) { return a[1]; }

    • @sledgex9
      @sledgex9 Месяц назад

      @@ysakhno Notice that you had to explicitly mention/enumerate the template arguments of your function parameter.

    • @Evan490BC
      @Evan490BC 26 дней назад

      An application I could think of would be in writing rank-independent numerical code using template metaprogramming, something akin to what Todd Veldhuizen did with Blitz++. The rank of the passed-in array would be the rank of the resulting tensor and its values the length of each dimension.

  • @PaulTopping1
    @PaulTopping1 Месяц назад +9

    C++ has gotten too complicated. There I said it. The story used to be that there was a simple, elegant programming language trying to escape from all the backward compatibility but I feel we've gone way beyond that. I am interested to see where Herb Sutter's CPP2 takes us.

    • @gregthemadmonk
      @gregthemadmonk Месяц назад +3

      "trying to escape from all the backward compatibility" I may have come to C++ too late, but was that ever the story? Wasn't C++ main selling point its huge backwards compatibility with C?

    • @snbv5real
      @snbv5real Месяц назад +3

      There are things that are complicated because of non technical reasons, but this is pure luddite talk, this is one of the things that you'd still be able to to in CPP2....

    • @PaulTopping1
      @PaulTopping1 Месяц назад +1

      @@gregthemadmonk Sure but it's the backwards compatibility of later versions of C++ with earlier versions of C++ that is the bigger burden. There are now multiple ways to do any one thing but only one of those ways is considered the "good" way.

    • @PaulTopping1
      @PaulTopping1 Месяц назад +3

      @@snbv5real CPP2 in is pure mode where old C++ constructs aren't accepted is designed to be the "much smaller and cleaner language struggling to get out" (Stroustrup). Wanting this is not Luddite talk but forward looking IMHO.

    • @gregthemadmonk
      @gregthemadmonk Месяц назад

      @@PaulTopping1 I'm not sure it is such a bad thing, to be completely honest. It allows developers not to rewrite libraries (including the standard library) every time a new way to do a thing drops. Sure, it is a burden to know all of the ways to do the same stuff, but a) it is, in my opinion, a fair trade for being able to use older libraries and not being stuck in dependency incompatibility hell and b) the "best" way to do a thing is usually the most recent one, I don't see much confusion arising here aside from people intentionally confusing themselves

  • @cubeman5303
    @cubeman5303 Месяц назад +2

    So basically a foot-gun.

  • @anon_y_mousse
    @anon_y_mousse 25 дней назад

    That's just about the most horrible name mangling scheme I've seen. Also, I kind of feel like CTAD is dumb in this particular context. It would make more sense if there were some container types that were more built-in to the language, but that's not really the C++ way. But this is one of the many reasons I've been implementing my own language.