Showing posts with label cpp_fun. Show all posts
Showing posts with label cpp_fun. Show all posts

Nov 6, 2024

[C++] include from /dev/stdin

When I was publishing Concurrent LRUCache which is used by my previous job at Linkedin, I used this feature provided by godbolt for demo, which is it could include headers through https protocol. 
e.g: https://godbolt.org/z/Y6he8z9Gf

Turned out this feature can be done through #include "/dev/stdin"

#include "/dev/stdin"

int main(){
  foo f{};
  (void)(f);
}

$ echo "struct foo {};" | clang++ -std=c++20 main.cc -Wall

Just replace the echo part with `wget`

Feb 16, 2021

[C++] [fun to know] const removal for declared function signature

Reference:
https://en.cppreference.com/w/cpp/language/function


Top-level cv-qualifiers are dropped from the parameter type (This adjustment only affects the function type, but doesn't modify the property of the parameter:
int f(const int p, decltype(p)*); and int f(int, const int*); declare the same function)


This also make function template deduction remove C.V from copy by value.

e.g


template<typename T >
void Run(T t);
const int i{42};
Run(i); // T is type of int

May 7, 2019

[C++] relax, it's just fun :-)

This thread is fun :-)
What was your latest discovery about C++?
From my point of view, the discoveries can be categorized into 3 parts:
Valid C++ code supports embedded URL in source code:
void fn()
{
https://www.google.com
cout << "Look no error!\n";
}

Jan 4, 2019

[C++][Rust] Reddit title: Rust 2019: Beat C++

https://www.reddit.com/r/rust/comments/acjcbp/rust_2019_beat_c/ed8ue2p/

This one is hilarious :-)

OP said this is 'the gross C++':

And provided a Rust version:

And s.o questioned, thus OP gives another post saying this is the original C++ code which he/she has eliminated (kinda like saving C++'s face or something, and he/she might not knowing C++14's _t, _v and misuse the enable_if) :
readers replied:

emm...
Unreasonable love makes people blind~ (yet love is unreasonable)

(However; I do hope there could be one syntax clean, UB friendly as well as efficient language on a par with C++ in the future, probably D, but still, not a language enthusiastic and a believer on 'using the right tool for the job')


Jan 23, 2015

[C++] Sean Parent for_each_argument style

for_each_argument—Sean Parent
#include <iostream>
#include <string>

struct ToString
{
    virtual std::string toString()=0;
};


struct Cpp final : ToString
{
    std::string toString() override
    {
        return "I am a C++ enthusiast.";
    }
};


struct Java final : ToString
{
    std::string toString() override
    {
        return "I currently coding in Java with stealth-mode project.";
    }
};


struct Python final : ToString
{
    std::string toString() override
    {
        return "I use Python for automation.";
    }
};


template<typename T>
void print(T&& t)
{
    std::cout << t.toString() << std::endl;
}


template <class F, class... Args>
void for_each_argument(F f, Args&&... args) {
    [](...){}( ( f(std::forward<Args>(args)), 0)... );
}


int main()
{
    for_each_argument(
        [](auto&& arg)
        {
            print(std::forward<decltype(arg)>(arg));
        },
        Cpp{}, Java{}, Python{}
        );
}

Jan 17, 2015

[C++] temporary string + int

gcc: get warning for `std::string("foo" + 42);`?


#include <iostream>

int main()
{
    std::cout << ("hi" + 42) << std::endl;
}
a.k.a
#include <iostream>
#include <string>

int main()
{
    std::cout << 42["hi"] << std::endl;
}

Dec 19, 2014

[C++] relax reading : Why C++ Sails When the Vasa Sank

Scott Meyers
https://www.youtube.com/watch?v=ltCgzYcpFUI

Vasa (ship)
Vasa (or Wasa)[1] is a Swedish warship built 1626–1628.
The ship foundered and sank after sailing about 1,300 m (1,400 yd) into her maiden voyage on 10 August 1628.

Why C++ Sails When the Vasa Sank? :-)

Is C++ heavily armed?
yeah, so why C++ still sails?

http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

* Destructors (C++)
RAII => general-purpose Undo
Basis of all resource management
Exception safety (emm, beyond(includes) C++11, destructors is by default noexcept)

* Templates (C++)
Generic programming (boost, STL etc.)
TMP
compile-time dimensional units analysis

* Overloading. (C++)
Generic programming.
Smart pointers
Function objects
Basis for lambdas in C++11

It's a language for library writers!


Pradigm agnosticism :-)
Procedural programming.
Free functions:
Basis of STL (C++)
Naturally models symmetric relationshops (C++)
Facilitates natural type extension (C++)

OOP
including MI. (C++)

Generic programming
Code works for any types (C++)

Functional programming.
Function objects(including closures) (C++)
TMP (ugly, but still~) (C++)

Unsafe programming
Pointers, casts, unchecked arrays, etc.
'Trust the programmer'

Safe programming
STL (C++)

Commitment to Systems Programming
Program speed

Program size.
static (C++, TMP. C, probably MACRO, type unsafe)
dynamic

Data layout
Driver
Comm protocols
Use with legacy code/system

Communicate with outsude entities
Hardware
OSes
Code in other languages

Zero overhead principle:
don'y pay what you don't need (C/C++)

Microcontrollers to supercomputers.
Implementation-defined sizes for e.g int/ptr
No assumptions about I/O capabilities

Hosted and unhosted.
No OS required

C/C++ compilers available almost everywhere.


Legacy code keeps working.
Standarization safequards investments in C++

All in all:
Compat with C
Very general features
Paradigm agnosticism
Commitment to systems programming
Dedication to backwards-compatibility