cppblog | Contains working code samples from my C blog | Application Framework library
kandi X-RAY | cppblog Summary
kandi X-RAY | cppblog Summary
Contains working code samples from my C blog, These examples might be slightly more elaborate than those from the blog, but the functionality is the same. C11 support is assumed, and the samples have been tested with the latest versions of g++.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of cppblog
cppblog Key Features
cppblog Examples and Code Snippets
Community Discussions
Trending Discussions on cppblog
QUESTION
I am using Visual Studio 2019 and Ubuntu 20.04.3 LTS for my Windows Subsystem for Linux (WSL) (setup for WSL was done following this MS dev post), and even with my Visual Studio's project
...ANSWER
Answered 2022-Feb-14 at 21:36As soon as I posted this question, I found the solution. The solution is to set the C Language Standard correctly by doing
QUESTION
I'm using Visual Studio to connect to a docker container via SSH for remote development using CMake as the build system. When Visual Studio launches the project for the first time or a CMakeLists.txt file has been altered, Visual Studio configures, allowing me to view, compile and run the CMake targets, and other CMake settings also become available in the UI (such as settings from the 'Project' drop down), however, when reopening the project, Visual Studio does not seem to detect the CMake Project or reconfigure automatically, meaning no targets are shown hence not possible to compile, or if shown don't work, and no settings for CMake are shown in the UI; I could theoretically update a CMakeLists.txt file which after relaunching again could cause a regenerate, causing Visual Studio to detect the project, show the targets and general settings relating to CMake in the UI, but this is not ideal and could cause the entire project to require recompiling.
Steps to reproduce:
- Create C++ CMake Project in Visual Studio 2022
- Run Docker container and add SSH connection info in Visual Studio options
- Replace the default 'x64-Debug' in CMakeSettings.json with 'Linux-GCC-Debug'
- Wait for automatic CMake generation or press Generate on the popup "C++ IntelliSense information may be out of date, generate the CMake cache to refresh.."
- Everything works perfectly now; the "Select Startup Item" menu button should be available to select, compile and execute the target/s specified from the CMakeLists.txt files. Other CMake buttons should now be available, (e.g. Project -> Configure Cache)
- Quit Visual Studio
- Open the project again in Visual Studio
- There does not appear to be a way to restore the working state, Visual Studio does not know the CMake targets anymore, we can no longer compile the project, nor run it. There appears no way to cause Visual Studio to reconfigure within the UI. The existing targets may appear, but no longer work.
I have then followed the tutorial for remote cache: https://devblogs.microsoft.com/cppblog/open-existing-cmake-caches-in-visual-studio/
Doing the steps:
- Replace configuration 'Linux-GCC-Debug' with 'Existing Cache (Remote)'
- Update cacheRoot to the path that contains CMakeCache.txt on the remote system
The regenerate popup shows ( "C++ IntelliSense information may be out of date, generate the CMake cache to refresh.." ), pressing regenerate works the first time, but then subsequent re openings of the project result in nothing happening again. No targets / executables to choose.
This issue does not occur for local MSVC CMake projects, the available targets are always shown immediately upon relaunch, along with all other Project settings being available in the UI.
Is this a bug in Visual Studio, or, is there something I'm missing so that Visual Studio always uses the existing cache to configure itself to make the targets available for compiling and executing, whenever I launch the existing project? How do IDE's typically detect CMake targets, perhaps knowing this could give a hint on solving the issue.
Visual Studio Version: 17.0.5
Docker File:
...ANSWER
Answered 2022-Feb-05 at 11:33This is a known bug in Visual Studio 2022 - https://developercommunity2.visualstudio.com/t/VS-2022-cant-reopen-cmake-project-on-re/1591862?entry=problem
Try updating to the latest version or downgrade to Visual Studio 2019.
QUESTION
Intel recommends using instruction prefixes to mitigate the performance consequences of JCC Erratum.
MSVC if compiled with /QIntel-jcc-erratum
follows the recommendation, and inserts prefixed instructions, like this:
ANSWER
Answered 2021-Dec-21 at 16:31A NOP is a separate instruction that had to decode and go through the pipeline separately. It's always better to pad instructions with prefixes to achieve desired alignment, not insert NOPs, as discussed in What methods can be used to efficiently extend instruction length on modern x86? (but only in ways that don't cause major stalls on some CPUs which can't handle large numbers of prefixes).
Perhaps Intel considered it worth the effort for toolchains to do it this way for this case since this would actually be inside inner loops, not just a NOP outside an inner loop. (And tacking on prefixes to one previous instruction is relatively simple.)
I now have some data point. The result of benchmarking for /QIntel-jcc-erratum
on AMD FX 8300 is bad.
The slowdown is by a decimal order of magnitude for a specific benchmark, where the benefit on Intel Skylake for the same benchmark is about 20 percent. This aligns with Peter's comments:
I checked Agner Fog's microarch guide, and AMD Zen has no problem with any number of prefixes on a single instruction, like mainstream Intel since Core2. AMD Bulldozer-family has a "very large" penalty for decoding instructions with more than 3 prefixes, like 14-15 cycles for 4-7 prefixes
It's somewhat valid to consider Bulldozer-family obsolete enough to not care much about it, although there are still some APU desktops and laptops around for sure, but they'd certainly show large regressions in loops where the compiler put 4 or more prefixes on one instruction inside a hot inner loop (including existing prefixes like REX or 66h). Much worse than the 3% for MITE legacy decode on SKL.
Though indeed Bulldozer-family is obsolete-ish, I don't think I can afford this much of an impact. I'm also afraid of other CPUs that may choke with extra prefixes the same way. So the conclusion for me is not to use /QIntel-jcc-erratum
for generally-targeted software. Unless it is enabled in specific translation units and dynamic dispatch to there is made, which is too much of the trouble most of the time.
One thing that probably safe to do on MSVC is to stop using /Os
flag . It was discovered that /Os
flag at least:
- Avoids jump tables in favor of conditional jumps
- Avoids loop start padding
Try the following example (https://godbolt.org/z/jvezPd9jM):
QUESTION
C# string's Splice method seems to copy remnants into an array of strings instead of just reading them. Is there a c++17 string_view equivalent to bypass copying?
For those not familiar with string_view, here is some background information.
The string_view family of template specializations provides an efficient way to pass a read-only, exception-safe, non-owning handle to the character data of any string-like objects with the first element of the sequence at position zero. (...)
From Microsoft's C++ Team Blog std::string_view: The Duct Tape of String Types:
string_view solves the “every platform and library has its own string type” problem for parameters. It can bind to any sequence of characters, so you can just write your function as accepting a string view:
...
ANSWER
Answered 2021-Apr-01 at 04:03ReadOnlySpan
could work.
Have a look at All About Span: Exploring a New .NET Mainstay
A second variant of Span, called System.ReadOnlySpan, enables read-only access. This type is just like Span, except its indexer takes advantage of a new C# 7.2 feature to return a “ref readonly T” instead of a “ref T,” enabling it to work with immutable data types like System.String. ReadOnlySpan makes it very efficient to slice strings without allocating or copying, as shown here:
QUESTION
When we define a variable of a built-in type too large to parse, Compiler/IDE (I'm using Visual Studio 2019) warns right away that the range has been violated, even before compiling the project - "integer constant is too large" if we're defining an integer.
...ANSWER
Answered 2021-Feb-20 at 19:07You can add a check to your constructor for the argument being inside a range, and then assert on that:
QUESTION
I'm using several libraries built through vcpkg (such as civet-web and prometheus-cpp), against my Visual C++ projects. When building x86 all is perfect, in x64 I get a bunch of linker errors:
error LNK2001: unresolved external symbol __CxxFrameHandler4
Searching online all references to this symbol/error are about specific projects, I cannot find what __CxxFrameHandler4 is and what problem this error is highlighting. I don't know if it's a problem with the way vcpkg is building the library, or a problem in my project or how to start looking for a solution.
I did find this blog article but it is in reference to a preview of VS2019, I cannot find any settings related to it: https://devblogs.microsoft.com/cppblog/making-cpp-exception-handling-smaller-x64/
If anyone can explain what this is all about it would be a big help.
...ANSWER
Answered 2021-Feb-03 at 13:43I faced the same issues when trying to install and use cpr with vcpkg. I wanted to use cpr library in a VS2015 project.
Reason: I had VS2019 installed. vcpkg uses latest version of toolset Visual Studio.
Resolution: Add your own triplet or change existing such a way that your specified toolset is used. Adding did not work in my case so I changed existing "triplet" files in triplet folder in vcpkg. I wanted vcpkg to use toolset that comes with VS2015 (It's V140)
Content of x86-windows.cmake file
QUESTION
Using clang++-11
with libstdc++-11
and clang-tidy-11
, I run into failures when linting the code with clang-tidy
- namely parsing error when dealing with the well-known sleeper std::this_thread::sleep_for(std::chrono::seconds(1));
.
ANSWER
Answered 2021-Jan-04 at 22:24Confirmed clang-tidy bug, https://bugs.llvm.org/show_bug.cgi?id=47511 . I'll leave the question as it is, perhaps someone will find the boost
trick useful until it's fixed.
QUESTION
Support for C++20 modules has been recently added in MSVC 16.8. If I'm not mistaken, one of the advantages of using modules is that circular import dependencies are now supported. However, the following test project still fails to compile in the latest version of the Microsoft compiler.
computer.ixx
ANSWER
Answered 2020-Nov-22 at 15:22If I'm not mistaken, one of the advantages of using modules is that circular import dependencies are now supported.
I am not aware of any version of module proposals, standards, or implementations that has ever supported circular dependencies. In pretty much every version of modules as a concept, going back to even pre-C++11 iterations, the module dependency graph has always been required to be a directed, acyclic graph.
It doesn't even make sense how that would work. If you import a module, then by definition, you need that module to be compiled before you can compile the module that imports it. But if that module requires your module too, there's no order in which to compile your modules. And you can't just "kinda" compile a module now and wait for something else to show up to fill in the gaps or something. C++ just doesn't work that way.
QUESTION
I've been playing with my own implementation of std::array and noticed libc++'s version uses explicitly defined operators for each of the comparisons (==,!=,<,>,<=,>=). I figured I could simplify my code by implementing C++20's spaceship operator (<=>). However, when I replaced the non-member comparison operators with auto operator<=>(const Array&) const = default;
in the struct body, GCC trunk indicated that the function was "implicitly deleted because the default definition would be ill-formed". Some investigation indicated that the raw array member was the culprit.
This webpage indicates that, "The compiler knows how to expand members of classes that are arrays into their lists of sub-objects and compare them recursively." And this SO answer indicates that only copyable arrays participate in the comparison synthesis.
Out of curiousity, I ran the code from the first link on Compiler Explorer. It also fails to compile on gcc trunk. However, clang trunk compiles the code successfully.
So, my question is: which compiler is right? Should the comparison be synthesized for member arrays or not?
...ANSWER
Answered 2020-Sep-30 at 14:44Should operator<=> synthesize array comparisons?
Yes. This is what the standard (final working draft) says:
[class.compare.default]
A defaulted comparison operator function (12.6.2) for some class C shall be a ...
The direct base class subobjects of C ... followed by the non-static data members of C, in the order of their declaration in the member-specification of form a list of subobjects. In that list, any subobject of array type is recursively expanded to the sequence of its elements, in the order of increasing subscript. Let xi be an lvalue denoting the ith element in the expanded list of subobjects for an object x(of length n), where xi is formed by a sequence of derived-to-base conversions (12.4.3.1), class member access expressions (7.6.1.4), and array subscript expressions (7.6.1.1) applied to x.
QUESTION
According to this post VS2017 supports color highlighting for both CMakeLists.txt as well as files with .cmake extension. In my projects Intellisense is not scanning and highlighting .cmake files. Is there a setting which needed to be set for this?
...ANSWER
Answered 2020-Sep-16 at 18:12Coloring appears after closing and reopening VS2017
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install cppblog
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page