std::variant: Why no viable overloaded '=' error - c++builder

I try to understand std::variant in C++Builder Rad Studio Rio 10.3.1 (CLANG version 5.0). In a console app, I simply have:
#include <stdio.h>
#include <variant>
int _tmain(int argc, _TCHAR* argv[])
{
std::variant<int, float> v;
v = 12;
return 0;
}
When I try to compile, it returns an error:
I am confused because it is an example that I found in several web pages. It is expected to compile without problems. The 12 value should not be candidate for float. However, using the CLANG compiler of Rad Studio it does not compile.

Related

vs code on MacBook // linker command failed with exit code 1 [duplicate]

I have looked at similar question on this site and googled why this is happening but have tried these solutionnot able to compile, difference between ncurses and curses, another compile error, but I am still having the problem with the error undefined reference to stdscr and wgetch the compiler is finding the library as far as I can figure out as I used find to locate the curses library and entered the full location in the #include line.
my code is as follows
#include <ncurses.h>
#include <stdio.h>
#include <string.h>
int first_line(char);
int main(){
char c = 0;
while((c = getch())!=EOF){
first_line(c);
}
return 0;
}
int first_line(char c){
if (c != '\n' && c != '\r'){
putchar(c);
do{
c = getch();
putchar(c);}
while( c !='\n');
}
else return 0;
return 0;
}
If you can point to what I've missed or am doing wrong I would much appreciate it.
ncurses.h is a header file (containing declarations), not a library (containing code that implements those declarations).
The error message you're seeing is from the linker, not from the compiler.
If you're using gcc, you need to add either -lcurses or -lncurses to the compiler command line (after your source file name).
For example, on my system I copied your source to c.c, and I can compile and link it using either
gcc c.c -o c -lcurses
or
gcc c.c -o c -lncurses

Embarcadero C++10.4.2 ignoring namespaces sometimes

I am an experienced C++ developer, but new to Embarcadero.
I found the following code compiles, and it should not. What settings can I use to make the compiler be conformant?
#include <fstream>
int main()
{
auto ifs = ifstream("hellp");
}

Eclipse CDT is reporting false erros while using std::index_sequence

UPDATED: Added complete example and compiler information
I have Eclipse 2019-03 (4.11.0) with CDT 9.7.0.20190309 and the build-in compiler reports false positive errors while using std::index_sequence in C++17:
#include <gtest/gtest.h>
#include <utility>
#include <array>
class Sample {
public:
template<std::size_t N >
std::size_t get_percentage( void ) {
return N;
}
template<std::size_t... Is>
inline std::array<std::size_t, sizeof...(Is)> calculate_percentages( std::index_sequence<Is...> ) noexcept {
return { this->get_percentage<Is>()... };
}
template<std::size_t N>
inline std::array<std::size_t, N> get_percentages( void ) noexcept {
return this->calculate_percentages( std::make_index_sequence<N>() );
/* ^^^^^^^^^^^^^^^^^^^^^ : Invalid arguments ' Candidates are: std::array calculate_percentages(std::integer_sequence) ' */
}
};
TEST( IntegerSequence, InvalidArgumentsError ) {
Sample test;
std::array<std::size_t, 5> data = test.get_percentages<5>();
for( int i = 0; i < 5; i++ ) {
std::cout << data[i] << std::endl;
}
}
int main( int argc, char ** argv ) {
testing::InitGoogleTest( &argc, argv );
return RUN_ALL_TESTS();
}
But the normal compilation succeeds without any problem.
My CDT GCC Built-in Compiler Settings in
Project Properties -> C/C++ General -> Preprocessor Include Paths, Macros etc. -> Providers is as follows:
${COMMAND} ${FLAGS} -E -P -v -dD -std=c++17 "${INPUTS}"
The same applies for CDT Cross GCC Built-in Compiler Settings.
Rebuilding the index does not helped in there.
The GCC version I am using:
gcc (Ubuntu 8.3.0-6ubuntu1) 8.3.0
Many thanks in advance to anyone willing to help...
The problem is caused by the fact that the standard library that comes with gcc 8 and newer uses a new compiler intrinsic called __integer_pack to implement std::make_integer_sequence (and related utilities like std::make_index_sequence).
Eclipse CDT's parser does not currently understand the __integer_pack intrinsic, and therefore it has trouble correctly parsing code that uses std::make_integer_sequence / std::make_index_sequence with these newer gcc versions.
I filed a CDT bug to track adding support for the __integer_pack intrinsic.
Meanwhile, a workaround you could employ is to use gcc 7 or older. If you need gcc 8 or newer to actually build your code, you could still tell Eclipse the look at the standard library headers of e.g. gcc 7 by replacing ${COMMAND} in the mentioned "built-in compiler settings" configuration with g++-7.
UPDATE: The Eclipse bug is now fixed, with the fix targeting CDT's 9.11 release (scheduled to be part Eclipse 2020-03).

win32-g++ error on for_each and lambda

I know this theme often turns up on stackoverflow, but the case here is different:
The following code compiles with MsVC++ 2013 but not with win32-g++. Is there any way of making it work with both compilers?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int>my_vector;
for(int i=0; i < 6; i++)
my_vector.push_back(i);
for_each( my_vector.begin(), my_vector.end(), [ ](int n){cout<<n;});
return 0;
}
The errors reported are
||=== stdtest, Debug ===|
D:\dev\CplusPlus\stdtest\main.cpp||In function 'int main()':|
D:\dev\CplusPlus\stdtest\main.cpp|13|warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]|
D:\dev\CplusPlus\stdtest\main.cpp|13|error: no matching function for call to 'for_each(std::vector<int>::iterator, std::vector<int>::iterator, main()::<lambda(int)>)'|
D:\dev\CplusPlus\stdtest\main.cpp|13|note: candidate is:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algo.h|4436|note: template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)|
D:\dev\CplusPlus\stdtest\main.cpp|13|error: template argument for 'template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)' uses local type 'main()::<lambda(int)>'|
D:\dev\CplusPlus\stdtest\main.cpp|13|error: trying to instantiate 'template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)'|
||=== Build finished: 3 errors, 1 warnings (0 minutes, 0 seconds) ===|
I should add that I am assuming that the C++11 standard is turned on because "enabled by default". In case I am wrong, I am having a hard time finding out how to change the compiler switches from Code::Blocks.
it turns out that I was misled by the statement that -sdd=C++11 was "enabled by default".
I ended up finding a way of turning it on with the Project > Build Options> compiler settings
and then I clicked on the checkbox "have g++ follow the c++11 ISO C++ language settings" checkbox.
For those who are working with QtCreator add the following line to the project file:
QMAKE_CXXFLAGS += -std=c++11

libpthread in mingw does not find the libraries

I am trying to compile the following program with mingw:
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <iostream>
#include <cstdio>
void *hello(void *id) {
int nid = *static_cast<int*>(id);
std::printf("Hello from thread %d\n", nid);
return 0;
}
int main(int argc, char **argv) {
pthread_t ids[2];
int *params[2];
for (int i = 0; i < 2; ++i) {
params[i] = new int;
*params[i] = i;
pthread_create(&ids[i], 0, hello, params[i]);
}
for (int i = 0; i < 2; ++i)
pthread_join(ids[i], 0);
for (int i = 0; i < 2; ++i)
delete params[i];
return 0;
}
using this command:
g++ -lpthread -ohello.exe hello.cc
And I get the following message:
C:\Users\XXXXXX~1\AppData\Local\Temp\cczPlv0w.o:hello.cc:(.text+0xad): undefined
reference to `_imp__pthread_create'
C:\Users\XXXXXX~1\AppData\Local\Temp\cczPlv0w.o:hello.cc:(.text+0xe9): undefined
reference to `_imp__pthread_join'
collect2: ld returned 1 exit status
But with an older version of MingGW I had no problems running pthreads programs. (This is just the simple of all the programs that failed, but basically everything that uses pthreads ends up with the same error, C and C++)
Move -lpthread to the end of that command:
g++ -ohello.exe hello.cc -lpthread
The order of the arguments is important. (Using -pthread throughout instead of -lpthread for linking is actually recommended, since it sets flags both for the preprocessor and the linker.)
Library specifications are position dependent with gcc, it will only bring in unresolved symbols at the point where the library is listed.
Because you haven't listed your main object file at that point, the only unresolved symbol is main. You need to move the library specifications to the point where there will be unresolved symbols they can satisfy.
I've never really understood why gcc chose this path since it sometimes leads to situations where you have to list libraries more than once (such as with circular dependencies). The only reason I've ever thought of is to keep control as to what libraries are allowed to resolve specific symbols.
I've seen more "intelligent" linkers where they simply batch up all the libraries till the end then go through them over and over again until either all symbols are satisfied or there is no chance that they can be. This saves a lot of effort but you do lose that aforementioned control.

Resources