I attempting to use a nested lambda as follows:
int main() {
auto x=[](int a){
return [a](int b){
return a+b;
};
};
int xx = x(1)(2);
(void)xx;
return 0;
}
However, the vs2013 compiler yields an error (C++11 language level):
[...] can't convert to int.
What is wrong?
Your code snippet is well-formed, and this is a compiler limitation or bug in the MSVC C++ compiler version used in VS2013. Note that VS2013 only claimed partial C++11 support, as per e.g. What's New for Visual C++ in Visual Studio 2013:
Improved ISO C/C++ Standards Support
Compiler
Supports these ISO C++11 language features:
[...]
Related
I am trying to port some C++17 code I made on ubuntu (gnu++11)
typedef boost::variant<int, float, std::string > Variant;
using Func = std::function<std::vector<unsigned char>(std::vector<Variant>)>;
void addexecutorfunc( Func callback, const auto&...args )
{
std::vector<Variant> vec = {args...};
executor.add(vec, std::move(callback));
}
this code compiles and works fine on ubuntu, but when trying to compile on windows with visual studio 2017(v141) [ISO C++ Latest Draft Standard(/std:c++latest)], then I get following:
error C3533: a parameter cannot have a type that contains 'auto'
I think perhaps it has to do with the Concepts lite not being implemented in current C++17 version or is this wrong?
If I could setup compiler to use auto as parameter and parameter packs, then that would be best, but if this is not possible, then I will have to rewrite my code to follow C++17 windows standard - any suggestions on how to do this without ending up in a template hell
void addexecutorfunc( Func callback, const auto&...args )
auto as a parameter to a (non-lambda) function is a GNU extension. It is not part of standard C++17, and is not supported by either of the other two major C++ compilers, Clang and MSVC. Rather unfortunately, GCC seems to allow it in -std=c++14 mode as well as with -std=gnu++14.
The standard C++ equivalent would be a function template
template <typename... Ts>
void addexecutorfunc(Func callback, const Ts&... args)
which should work as expected.
I tried to get myself into C++ and purchased the book "Programming - Principles and Practice Using C++" by Bjarne Stroustrup.
When I tried to get compile the following source code:
#include "std_lib_facilities.h"
int main(){
cout<<"Hello, World!\n";
keep_window_open();
return 0;
}
I am getting following compile error:
In file included from /Users/hypertrooper/Documents/Programming - Principles and Practice Using C++/hello_world.cpp:1:
std_lib_facilities.h:71:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = typename std::vector<T>::size_type;
^
std_lib_facilities.h:102:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = std::string::size_type;
^
std_lib_facilities.h:107:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:113:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:213:107: error: expected '(' for function-style cast or type construction
inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
~~~~~~~~~~~~~~~~~~~~~~~~~~^
std_lib_facilities.h:222:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Value_type = typename C::value_type;
^
std_lib_facilities.h:225:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Iterator = typename C::iterator;
^
6 warnings and 1 error generated.
I get it that my compiler is not using C++11 feature, but I do not know how I can update the compiler. I should let know that I am using a MacOSX (10.10 Yosemite) and tried to compile with xCode and textmate. I even tried to follow this tutorial(https://wiki.helsinki.fi/display/HUGG/Installing+the+GNU+compilers+on+Mac+OS+X), but it did not helped. (At least when I tried to compile with text mate)
I hope you are able to help me. :(
If you are on a Mac or Linux, the compiler is usually g++ or clang; to access C++11, just specify -std=c++11 as an option when invoking the compiler (Assuming that you have an up-to-date version).
What you need to do is open the project settings -> Build Settings and set C++ Language Dialect to C++11 and the C++ Standard Library to libc++ (LLVM C++ standard library with C++11 support).
From your build settings, adjust the C++ Language Dialect to C++11...
Also make sure you are using the LLVM C++ library, as it has full C++ 11 support.
I'm using Visual Studio 2013 Express.
class B{
public:
vector<char>& a;
int& b;
B(vector<char>& i,int& c) :a(i),b(c) {}
};
int main(){
int l=3;
vector<char> h;
shared_ptr<B> bb (new B(std::move(h),l));
return 0;
}
Why can the code be accepted?When I changed the argument l to std::move(l),the compiler will complain "cannot convert argument 2 from 'int' to 'int &'".
This is a language extension available in the Visual C++ compiler and has existed for quite some time now. The extension allows you to bind an rvalue (tempoarary) to a non-const reference and extend the lifetime of value as if you were binding to a const reference.. If you enable warning level 4 or explicitly enable warning C4239 the compiler will alert you any time the extension is used.
The documentation for C4239 includes an example that is similar to what's in your question.
I'm having trouble working with lambda functions in the Intel compiler, in particular, the following code won't compile:
template<typename T>
std::function<T (int)> make_func(T x) {
return [=](int index) -> T
{
return x;
};
}
The error I get is
error: namespace "std" has no member "function"
The code compiles and runs fine on my Mac, (macports gcc version 4.5). The error is at work, where we use the Intel compiler version 11.1. It does accept lambda functions (with the -std=c++0x option), such as:
auto lam = [=](int j) -> int {
printf("testing for lambdas: %d\t%d\n", n, j);
return n;
};
int g = lam(7);
The version of gcc installed at work is 4.1.2, so I'm guessing that the standard library is old?
/bin/libc.so.6
says it's version 2.5 compiled with gcc 4.1.2.
Is there a way around this?
thanks in advance for any help
I get the same behavior with icc 11.1 on a system where gcc 4.5.2 is installed.
g++'s header <functional> is protected with #ifdef __GXX_EXPERIMENTAL_CXX0X__ which is not defined when icc is used.
I would consider switching to boost::function in this setup, which of course works with icc.
Well, the code shown doesn't include a single header. And yet you refer to the standard library std::function.
So no, it doesn't compile. As with any other part of the standard library, you need to include the header where std::function is defined: <functional>.
In the following example:
void foo (double *ptr)
{
const double * restrict const restr_ptr=ptr;
}
I get this error:
error: expected a ";" const double * restrict const restr_ptr=ptr;
^
I compile with -std=c99, using gcc 3.4
Any Ideas?
In C++, restrict is not a keyword (except for Microsoft extensions). It doesn't mean what it does in C. It looks as though you tried to apply C99 mode to your C++ compiler. Use a C compiler to compile C code, and use a C++ compiler to compile C++. Neither language is a subset of the other.