How do I import C++ 20 <format> module? - c++

I am using Visual Studio 2019 (Community edition) running on a Windows 10 machine. I have created a simple console application and I want to import the format module so that I can use something like std::format(). I get an error that 'cannot find header 'format' to import. My code is based on a book by Horton and van Weet titled 'Beginning C++ 20' which starts with two lines namely: import iostream (this is contained in angled brackets) followed by a line which says import format also contained within angled brackets. Maybe let me rephrase my question. The code generated by Visual Studio says #include iostream but does not allow me to #include format. So, when do I use #include and when do I use import?

While waiting for MSVS support of std::format, you can use the fmt library that is the basis for the std::format. This can be found at https://github.com/fmtlib/fmt. It is compatible with the C++20 standard but does include additional features. Add the library to your source and use fmt::format instead of std::format. It'll be easy to convert this to std::format once fully supported.

When using Visual Studio 2019/2022 to run C++20 code, you need go to: Project->'source code name' Properties and:
Configuration Properities->General->C++ Lanaguage Standard: ISO C++20 Standard (/std:c++20).
Configuration Properities->C/C++->Language->C++ Lanaguage Standard: ISO C++20 Standard (/std:c++20).
Configuration Properities->C/C++ ->General -> Scan Sources for Module Dependencies: Yes.
These three options must be done.

Related

VSCode C++ Intellisense can't discern C++20 features

I try to run codes like
#include <string>
#include <iostream>
int main() {
std::string str = "This is a string";
std::cout << str.starts_with("name");
}
But intellisense will give out an error
"std::__cxx11::basic_string<char, std::char_traits,
std::allocator>" has no member "starts_with" C/C++(135) [6,9]
And It still can be build and produce a correct result.
Also it can find implementation in header file.
But the macro __cplusplus is defined as 201703L
I've already added a command -std=c++20 when building, why this happened?
Compiler: minGW 11.2 compiled by msys2
Assuming you are using Microsoft's C/C++ extension, you must configure the extension to use C++ 20 standard for intellisense.
The easiest way to do this is to add the line "C_Cpp.default.cppStandard": "c++20" to your settings.json file. You can also find the setting in the GUI under the name "Cpp Standard". Selecting c++20 from its dropdown will achieve the same result.
Note that this setting is, by default, set as a global user defaults. You can configure it per-workspace by selecting the Workspace tab in the settings GUI and changing that Cpp Standard dropdown to c++20.
As for why adding the -std=c++20 flag didn't work: -std=c++20 just tells your compiler which standard to use to build your code. 'Intellisense' does not receive this flag because it is a separate tool from the compiler and is therefore not required to support all the standards the compiler supports. It may support less even, although Intellisense tools usually support as current a standard as possible. Therefore the language standard for Intellisense must be configured separately from the compiler (in this case).
Final Note: After changing the setting, try closing and re-opening VS Code. In my experience changing the language standard setting can cause some weirdness to happen. Closing and re-opening VS Code seems to ensure the setting changes take full effect.

when I try and use std::filesystem in vs 2019 I get an error

I am using Microsoft Visual Studio 2019, and whenever I type std::filesystem there's a red bar under "filesystem", with the messages
namespace "std" has no member "filesystem"
and
name followed by '::' must be a class or namespace name
when I don't have "::" in front of it and when I do respectively.
I have included filesystem so I don't see why it doesn't work.
I hope I provided enough information, if not just ask in the comments. if you can't help me with that, maybe you could help me find an alternative to std::filesystem::recursive_directory_iterator.
The filesystem library was added in C++17 and VS2019 starts in C++14 mode by default.
Open Project\<project name> Properties
Select Configuration Properties\General
To the right, there is a field named C++ Standard, select one of these:
ISO C++17 Standard (/std:c++17)
Preview - Features from the latest C++ working draft (/std:c++latest)
Then #include <filesystem> and you should be good to go.

How to specify c++ 14/17 with bazel.rc (tool specific to using GCC /G++, project not open to other tool chains)

Within my bazel.rc file for the C++ Bazel build options, I have specified:
'build --cxxopt="-std=c++1y"'
When I try to change to build --cxxopt="-std=c++14", bazel Throws error of :
''build --cxxopt="-std=c++14"' is not an option'
, with a cursor below 4 in c++14. If I change the 4 to y (c++1y), it compiles with no problem. I tried adding 'std:make_unique' in my code (c++14 addition) , and it clearly does not compile (make _unique is not part of std) which is assume is complaining that c++14 is not set as the standard, hence no make_unique for me.
What is the latest supported version of C++ in bazel? More specifically, how do I enable C++14 / C++17 (and even C++2x) for Bazel build, it those are supported? Thanks!
NOTE: this is not the same as How to set C++ standard version when build with Bazel??. (I am not asking about cpp 11 and I am asking about a very specific toolchain -- GCC/g++) I am not using any of the tools suggested by that thread. I am using GCC / g++ and am restricted in not being able to use the tools suggested by the answer in the previous question, I've RTFMd and googled. Thanks in advance.

xcode fails to build C++ with Unknown type namespace

I have created a C++ project in Xcode 8.2.1 with a Bridging Header file.
I have added a Point3.hpp and Point3.cpp file into a shared folder.
The types for the cpp and cpp files (in the identity and type inspector) are Default - C++ Source and Default - C++ Header respectively.
The LLVM 8.0 Language settings are:
C++11 [-std=c++11]
libc++ (LLVM C++ standard library with with C++11 support)
When I try to build the project, I find that I get the following error:
I have tried searching to find information to resolve this issue, but most seem to be related to compiling source that is a combination of objective-C and C++ whereas this project was setup as a Swift3 project with C++ bridge. Source code
ex3-Bridging-Header.h
#import "../../../Shared/ex3/math/Point3.cpp"
Point3.cpp
#include "Point3.hpp"
using namespace EX3;
Point3::Point3()
{
} ...
Point3.hpp
#ifndef EX3_POINT3_HPP
#define EX3_POINT3_HPP
namespace EX3
{
class Point3
{
public:
Point3(); ...
compiler settings
After continuing looking, I finally managed to find a working solution. There may be other ways to achieve this but I fixed it by routing the C++ through an Objective C layer. Whilst this seems a little bit clumsy (as it adds an extra bridge layer), as of Swift3, it is reported that there is no direct interop layer to C++.

please provide the location of the "\msdev\include " directory. to download

I installed EDG compiler in Windows i.e in "win32". After installation I am trying to run this simple code:
using namespace std;
int main()
{
cout<<"OM";
return 0;
}
I am getting errors like unidentified cout, etc
Actually I didn't give path dev libraries : there in read me file it is to give. Later I did gave path to libraries(VS C++ 2010)
Please provide me the MSDEV /include header files to download.
Please provide the location of the "\msdev\include" directory to download.
edit: in readme file they gave "No stream
I/O library is included; this is just very basic support."
if we gave iostream.h also .it shows ..many erros ..regarding that?
EDIT: Otherwise any one
Provide link to download EDG compiler (C++) for windows.Free ware
who already working successively with this....
As far as I see from their page, EDG do not provide a full compiler, but only a front-end; it does not include neither an optimizer/code generator neither a standard library. You simply cannot use just EDG to produce an executable.
You can find several free implementations of the C++ standard library (e.g. libstdc++ from GNU, to which I suppose you should add glibc for the C library subset), but without at least a code-generator backend all you can get from the front-end is the AST of the code gave in input.
Moreover, EDG C++ it's not free, neither it is sold to individuals; EDG license only the source code and only to corporations for a 40K$-250K$ price range. The links you're asking in your question would be illegal.
If you just need a compiler for Windows, there are several great alternatives, both free and not-free, some are listed e.g. in this question.