C++ - Reddit
225 subscribers
48 photos
8 videos
24.6K links
Stay up-to-date with everything C++!
Content directly fetched from the subreddit just for you.

Join our group for discussions : @programminginc

Powered by : @r_channels
Download Telegram
made the application misbehave. That includes every possible issue, not just C++ exceptions being thrown. And that is very powerful.

I simply have this standard SE aware main in each and every of my WIN apps. That is **not** very complicated and has a lot of benefits.

Please do understand SE is inbuilt in Windows and in the CL compiler and there are SE intrinsics too. Including the [keywords added](https://docs.microsoft.com/en-us/windows/win32/debug/abnormaltermination) to both C and C++.

### 1.2.2. SEH friendly, standard Windows C++, you can do too

Now you know how that mechanism and design works. You can do that too in your C++ Windows, SEH friendly code.

```cpp
namespace my {
inline namespace constants {
enum error_type {
error_ctype,
error_syntax
};
} // my namespace constants

// my::error does not inherit from std::exception
struct error final
{
error () = default ;
const char * msg_ {"unknown"};
const char * what () const noexcept { return msg_ ;
explicit error( constants::error_type ex_ ) noexcept : err_(ex_)
{ /* here make the message by the code */ }
constants::error_type code() const { return err_ ; }

#ifndef MY_RAISE
#ifdef _DEBUG
#define MY_RAISE(x) _invoke_watson(_CRT_WIDE(#x), __FUNCTIONW__, __FILEW__, __LINE__, 0)
#else // _DEBUG
#define MY_RAISE(x) _invoke_watson(nullptr, nullptr, nullptr, 0, 0)
#endif // _DEBUG
#endif // ! DBJ_RAISE

// we raise the SEH from here
// and through calling _invoke_watson
[[noreturn]] void raise() const {
DBJ_RAISE(*this) ;
}

} ; // eof error
```
We will always use the `MY_THROW` macro
```cpp
#if _HAS_EXCEPTIONS == 0
MY_THROW(x) x.raise() ;
#else
MY_THROW(x) throw x ;
#endif
```
Lastly, there is always a function that does the raise, and does not return. A level of indirection to improve the change-ability of the design always exist:
```cpp
[[noreturn]] inline void __cdecl
error_throw (const constants::error_type code_)
{
MY_THROW( error(code_) ) ;
}
} // eof my ns
```
To enjoy the SEH benefits, your Windows main should always be "SEH enabled"
```cpp
// The "Standard" main()
// build without /EHsc or any other /EH
// or use the /kernel switch
extern "C" int main (int argc, char ** argv)
{
__try
{
__try {
my::error_throw( constants::error_type::error_syntax ) ;
}
__finally {
// will be always visited
}
}
__except ( 1 /* 1 == EXCEPTION_EXECUTE_HANDLER */ )
{
// your code here
}
return 0 ;
}
```
That will always work c++ exception or no C++ exceptions. In case you want C++ exceptions you can not mix that in the same function, so you just call some entry point into your standard C++ app from the `main()` above.

If you build with `/EHsc` your app will be: c++ exceptions **and** SEH enabled. If built without, you will be intrinsic SEH enabled. SEH intrinsics are in `<excpt.h>`

> In Windows C/C++, SEH is always there

### 1.2.3. COM, C++ and /kernel builds

["Compiler COM Support"](https://docs.microsoft.com/en-us/cpp/cpp/compiler-com-support?redirectedfrom=MSDN&view=vs-2019) was designed, implemented and tested to also use C++ exceptions. Not SEH.[MSFT COM](https://en.wikipedia.org/wiki/Component_Object_Model) predates C++ standardizations. Just like SEH does.

In 2020 Q4, if and when attempting C++ `/kernel` or no `/EH` builds, C++ exceptions are replaced with SEH.

You need to know right now [things are happening in there "by accident"](https://github.com/MicrosoftDocs/cpp-docs/issues/2494#issuecomment-701200395). Pleas do not rely on `<comdef.h>` `/kernel` or not `/EH` combination until further notice.

"Compiler COM Support" and SEH is accidental combination that works 2020 Q4.

MSFT ["Compiler COM Support"](https://docs.microsoft.com/en-us/cpp/cpp/compiler-com-support?redirectedfrom=MSDN&view=vs-2019) I do like, it is C++, it is simple and it "just works". But these days it is meeting the not-so-simple standard C++, and WG 21
representatives and followers, inside MSFT. So somebody has decided \<comdef.h> and friends will firmly stay inside c++ exceptions territory. Which is very wierd since all of the MS STL does easily switch between SEH and C++ exceptions.

## 1.3. Conclusion(s)

Many people are, but I am not that perturbed with MS STL apparent duality and ability to transparently switch of C++ exceptions easily.

Neither many projects are usurped too. Why not? Here are the facts. Most of the customers are on Windows. WIN32 is primary Windows API. WIN32 is C. Same as UTF16 (`wchar_t`) is standard on windows, not `char`, SEH is standard on Windows, not C++ exceptions.

For Windows code, SEH is the OS norm.

https://redd.it/j4x9ge
@r_cpp
Same entity, different type? Covering a certain entity in C++ where the type of the entity changes depending on the context.
https://dfrib.github.io/same-entity-different-type/

https://redd.it/j4xtf5
@r_cpp
Json for c++20 modules

Hello, is there any json library that will work with modules? Including headers for known json libraries cause compiler to crash. Is there any working or we need to wait for them to be ported to module version?

https://redd.it/j4zx89
@r_cpp
Transform that requires more inputs?

What is the architectural idea behind the limited number of input iterators to all of the std numeric-/algorithms? This design makes the naked for-loop a much better option than algorithms for problems that cannot be reduced to just a couple of inputs.

Take transform. Its greatest signature is:

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3, class BinaryOperation > ForwardIt3 transform( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt3 d_first, BinaryOperation binary_op );

(From cppreference)

What is the point of this limited number of input and the requirement of just two inputs? Why isn't the signature something like:

template< class ExecutionPolicy, class MultivariateOperation, class ForwardIt, class ... InputsOutputIt > auto transform( ExecutionPolicy&& policy, MultivariateOperation op, ForwardIt1 first1, ForwardIt1 last1, InputsOutputIt ... io);

Where MultivariateOperation requires operator() to take the basic type of ForwardIt followed by all other but the last basic types of InputsOutputIt, and the last one is used to be the output so that the output auto can be set as expected?

https://redd.it/j51yfz
@r_cpp
How to customize proc.c in minix for implementing SJF algorithm?

Hi, im in need of some help!

I was tasked by my professor to change the [proc.c](https://github.com/jncraton/minix3/blob/master/kernel/proc.c) and [proc.h](https://github.com/Stichting-MINIX-Research-Foundation/minix/blob/master/minix/kernel/proc.h) so the MINIX 3 OS may switch from FIFO (first in first out) to SJF (shortest job first).

I have reached the conclusion that the alteration could be done in the `void enqueue(rp)` function, based on this Stack Overflow post: [How to customize proc.c in minix for implementing FCFS algorithm?](https://stackoverflow.com/questions/9854646/how-to-customize-proc-c-in-minix-for-implementing-fcfs-algorithm)

But I can't seem to find a way to implement the SJF, can anyone help me?

https://redd.it/j4yfip
@r_cpp
Tool for "Go to definition"

Hello,

as you know many IDE's/Editors feature a "Go to definition" command that finds the definition of whatever the cursor is currently on. If you have a large and complex program, these tools will/can only work if they know the build process.

What tool can you recommend my for "Go to definition". I need one that not only works in most cases, but in all cases(so it needs to know the build process). The build process is a fork of gnu make, but I have a [json compilation database](https://clang.llvm.org/docs/JSONCompilationDatabase.html) that I generated using [bear](https://github.com/rizsotto/Bear).

&#x200B;

I basically need the rust language server, but for c++ instead of rust.

https://redd.it/j57se3
@r_cpp
Audio library for wakeword detection

I have a NN that I've converted to a header using tensorflow-lite -- now I want to build an app that pulls in audio from my laptop's mic so I can predict on it using my model.

Is juce the best API to use for this?

I'd also like to plot the audio in real-time (or close to it). What library do you recommend for this?

I'm new to cpp but have plenty of experience with matlab/python. Any suggestions are apprecfiated - thanks!

https://redd.it/j57cxq
@r_cpp
C++ Primer (5th Edition) Vs Programming: Principles and Practice Using C++ (2nd Edition) - recommend?

I'm doing some self-teaching and I was recommended these two books. Does anyone have any experience with either?

https://redd.it/j59hfp
@r_cpp
Place header files in separate folder or not?

Are there any reason to place header files in a separate directory if code isn't going to be used in other projects?

https://redd.it/j5ajhs
@r_cpp
Understanding and correcting a function

Hi Guys, I am trying to understand how to encrypt a phrase/some characters, but I can't do it. I do not seem to get what am I doing wrong, obviously, the function doesn't work. Can someone pleasee point me/explain to me what am I doing wrong and how the function should work?

# include <iostream>

using namespace std; void criptare(char sir\[\]);

int main() { char sir\[\] = "abcdefghijklmnoprsttrewsr"; criptare(sir); cout << sir << endl;

include <iostream>
using namespace std; void criptare(char sir[]);

int main() {
char sir[] = "abcdefghijklmnoprsttrewsr";
criptare(sir);
cout << sir << endl;
return 0;
}

&#x200B;

&#x200B;

void criptare(char sir[])
{
char ref[] = "abcdefghijklmnopqrstuvxyz";
char coresp[] = "knirxbdetjmaopsczguvlyhqf";
int i, j;
for (j = 0; j < strlen(sir); j++);
{

for (i = 0; i < strlen(ref); i++);
{
if (sir[j] == ref[i])
{

sir[j] = coresp[i];
}


}

}

}

EDIT: many thanks!

https://redd.it/j5opzh
@r_cpp
Event Based Programming

I come from a iOS and C++ background. Always wondered how the dispatching of the events worked under the hood in iOS. I bought myself a Teensy 4.0 so I can start from the very basics implementing the event library.

This learning journey will involve me writing a framework that will enable taking callbacks on a particular pin on events such as button pressed, button long pressed, etc.

I am not asking how teensy works. I am more interested in the architecture of the framework. And also being able to expand to various events later on.

Will someone guide me through this journey.

https://redd.it/j5rfer
@r_cpp