I try to increase C++ version in XCode MacOS BigSure(11.0.4).
And optional header is found good but when I try to use std::optional I get an error No template named optional in std. In optional header I find implementation but part with
namespace std {
template <class T> optional;
...
}
is under the /**/.
I increase version in settings to C++17 and standard library to libc++(LLVM with C++11 support). clang version is 12.0.5.
How can i fix this trouble?
Related
According to cppreference.com ( https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_features ) Clang has partial support of C++20 coroutines since version 8:
But if in Clang trunk (which is upcoming version 13) I write
#include <coroutine>
it results in the error( https://gcc.godbolt.org/z/rTfjbarKz ):
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/coroutine:334:2: error: "the coroutine header requires -fcoroutines"
#error "the coroutine header requires -fcoroutines"
And if I add -fcoroutines flag in the command line, then Clang complains( https://gcc.godbolt.org/z/qMrv6nMzE ):
clang-13: error: unknown argument: '-fcoroutines'
Is there any way to start using C++20 coroutines in Clang?
Note that the first error is in the GCC standard library, and from that it can be deduced that the -fcoroutines option is for GCC not Clang.
To build with the Clang libc++ you need to add the option -stdlib=libc++ instead. But that will lead to the <coroutine> header file not being found instead.
Since Clang coroutines is still in the "experimental" stage you have to include <experimental/coroutine>.
So there are two things you need to change:
Use the Clang libc++ (-stdlib=libc++)
Include the experimental header file (#include <experimental/coroutine>)
Also note that since coroutines are experimental, the symbols defined in the header file will be in the std::experimental namespace.
GCC supports the -fconcepts switch, providing experimental core language concept features.
Is there also a way to use (an experimental version of) the standard library implementation?
It would be great to have std::derived_from, std::convertible_to, std::destructible and friends available when designing concepts.
#include <concepts>
template<typename T>
concept Fooable = requires(T f) {
{ bar(f) } -> std::convertible_to<float>;
};
The question does not clarify the version of GCC. Actually starting from GCC 10 the concepts are supported with -std=c++20 command-line option both by the compiler and standard library implementation (libstdc++) coming with it. Demo: https://gcc.godbolt.org/z/9G5Y1KEeT
In our codebase we use extensively boost::intrusive_ptr and after moving some headers around I started to get unexpected compilation errors from clang:
In file included from C:/code/Signal.cpp:1:
In file included from C:/code/signal.h:3:
In file included from c:/boost_1_56_0/include/boost/smart_ptr.hpp:26:
In file included from c:/boost_1_56_0/include/boost/intrusive_ptr.hpp:16:
c:/boost_1_56_0/include/boost/smart_ptr/intrusive_ptr.hpp:68:34: error: call to function 'intrusive_ptr_add_ref' that is neither visible in the template definition nor found by argument-dependent lookup
if( px != 0 && add_ref ) intrusive_ptr_add_ref( px );
There are similar issues reported elsewhere.
Our code that uses boost::intrusive_ptr was written a while ago and defines all these freestanding function (like intrusive_ptr_add_ref) in boost namespace. After checking what was the reason of compilation errors that I started to get, it appears that I should define these functions in my namespace instead. From description of intrusive_ptr:
On compilers that support argument-dependent lookup,
intrusive_ptr_add_ref and intrusive_ptr_release should be defined in
the namespace that corresponds to their parameter; otherwise, the
definitions need to go in namespace boost.
I assume most modern compilers do support ADL, however, I couldn't find definite information on what compilers support and what do not support ADL.
So, here's the question: what are these compilers that do not support ADL. I'm primarily interested in microsoft compilers (VS2012, VS2015), and faily recent versions of gcc and clang.
According to this page, ADL support was added in Visual Studio 2008.
With some difficulty, I was able to build EGCS 1.1, the first GCC version that claimed to support namespaces, released in 1997. I tested it by compiling and running the following program:
#include <stdio.h>
namespace foo {
class bar {};
void baz(bar&) {
puts("foo::baz");
}
}
void baz(const foo::bar&) {
puts("::baz");
}
int main() {
foo::bar bar;
baz(bar);
}
It printed foo::baz, which is correct. So basic ADL functionality existed in GCC even back in those days. I doubt you could even build Boost using a compiler that old.
Clang 2.7 also gets it right. Note that LLVM 2.7, released in 2010, was the first version of LLVM to ship with C++ support in Clang enabled by default. There are older versions of the Clang C++ frontend, but I don't think they were widely used, and I am too lazy to try to build them from SVN.
I would like to write a "portable" C++ library in Clang. "Portable" means that I detect (in C preprocessor) what C++ features are available in the compilation environment and use these features or provide my workarounds. This is similar to what Boost libraries are doing.
However, the presence of some features depends not on the language, but on the Standard Library implementation. In particular I am interested in:
type traits (which of them are available and with what spelling)
if initializer_list being constexpr.
I find this problematic because Clang by default does not use its own Standard Library implementation: it uses libstdc++. While Clang has predefined preprocessor macros __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, they are hardcoded to values 4, 2, 1 respectively, and they tell me little about the available libstdc++ features.
How can I check in Clang preprocessor what version of libstdc++ it is using?
Clang does come with its own standard library implementation, it's called libc++. You can use it by adding -stdlib=libc++ to your compile command.
That being said, there are various ways to check Clang/libstdc++ C++ support:
Clang has the __has_feature macro (and friends) that can be used to detect language features and language extenstions.
Libstdc++ has its own version macros, see the documentation. You'll need to include a libstdc++ header to get these defined though.
GCC has its version macros which you already discovered, but those would need to be manually compared to the documentation.
And also, this took me 2 minutes of googling.
This is what I think would help. It prints the value of the _LIBCPP_VERSION macro:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
cout<<"Value = "<<_LIBCPP_VERSION<<endl;
return 0;
}
Compile it again the version of clang you want the info for.
What's a good/best way to toggle some function declarations based on VS version?
context: I need to build on linux and windows and keep hitting cases where GCC compiles fine but VS2012 lacks some c++11 features. I'd like to #ifdef out the unsupported syntax so that it can be used right now on linux and easily enabled later for VS2013 support.
features like default function template parameters are supposedly good with vs2013: http://blogs.msdn.com/b/vcblog/archive/2013/06/27/what-s-new-for-visual-c-developers-in-vs2013-preview.aspx
//syntax fail; can this be wrapped with "If GCC or VSVERSION > XYZ" ?
template <typename T/*=std::complex<double>*/> void setImpedance(T impedance);