Trying to compile the following call to boost::fusion::invoke in boost-1.56 fails in Visual Studio 2013 but there is no error when compiling with Visual Studio 2012.
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/fusion/functional.hpp>
void Function( int & output )
{
output = 12;
}
int main( int, char ** )
{
boost::fusion::vector1< int > parameters;
boost::function< void( int & ) > function = &Function;
boost::fusion::invoke( function, parameters );
return 0;
}
The compiler output is:
boost\fusion\functional\invocation\invoke.hpp(205): error C2039: 'type' : is not a member of 'boost::result_of<Function (const int &)>'
with
[
Function=boost::function<void (int &)>
]
boost\fusion\functional\invocation\invoke.hpp(163) : see reference to class template instantiation 'boost::fusion::detail::invoke_impl<boost::function<void (int &)>,Sequence,1,false,true>' being compiled
with
[
Sequence=const boost::fusion::vector1<int>
]
main.cpp(16) : see reference to class template instantiation 'boost::fusion::result_of::invoke<boost::function<void (int &)>,const boost::fusion::vector1<int>>' being compiled
It's failing when trying to instantiate boost::result_of with a const Sequence. I've looked in boost\fusion\functional\invocation\invoke.hpp and there are two overloads of boost::fusion::invoke, one is const and the other is non-const.
I think that the Visual Studio 2013 compiler is attempting to instantiate the const version even though that is not the one that should be called. If I comment out the const version in invoke.hpp the example compiles fine.
Is this a bug with Visual Studio 2013 or boost-1.56?
Is there any workaround for this problem without modifying the boost sources?
I suspect the default for BOOST_RESULT_OF has changed for VS2013.
#define BOOST_RESULT_OF_USE_TR1
makes it compile. Chances are this bug is known and has been fixed in trunk, but you might want to report it still
Related
I want to use a lambda expression as custom Compare for a std::set of integers. There are many answers on this site explaining how to do this, for example https://stackoverflow.com/a/46128321/10774939. And indeed,
#include <vector>
#include <set>
#include <iostream>
int main() {
auto different_cmp = [](int i, int j) -> bool {
return j < i;
};
std::set<int, decltype(different_cmp)> integers(different_cmp);
integers.insert(3);
integers.insert(4);
integers.insert(1);
for (int integer : integers) {
std::cout << integer << " ";
}
return 0;
}
compiles and outputs
4 3 1
as expected. However, when I try to put this set in a vector with
std::vector<std::set<int, decltype(different_cmp)>> vec_of_integers;
vec_of_integers.push_back(integers);
the compiler complains. I'm using Visual Studio 2017 and I get different compiler errors depending on the surrounding code. In the above example, it's
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\utility(77): error C2664: 'void std::swap(std::exception_ptr &,std::exception_ptr &) noexcept': cannot convert argument 1 from '_Ty' to 'std::exception_ptr &'
1> with
1> [
1> _Ty=main::<lambda_48847b4f831139ed92f5310c6e06eea1>
1> ]
Most of the errors I've seen so far with this seem to have to do with copying the set.
So my question is:
Why does the above code not work and how can I make it work, while still using a locally defined lambda?
This seems to be a bug in MS compiler as it compiles well with GCC and Clang.
To make it work in MS Compiler (Visual Studio 2017) you can do this:
std::vector<std::set<int, decltype(different_cmp)>> vec_of_integers{integers};
This compiles cleanly. See here.
Why does this simple c++ code snippet do not compile?
#include <algorithm>
#define SIZE (1000)
struct S {
int *vect;
};
int main() {
struct S* s = static_cast<struct S*>(malloc(sizeof(struct S)));
s->vect = static_cast<int*>(malloc(sizeof(int) * SIZE));
for(int i = 0; i < SIZE; i++) {
s->vect[i] = i;
}
std::sort(s->vect, s->vect + SIZE);
}
The compiler returns the following error related to the std::sort call
1>C:\Program Files (x86)\Microsoft Visual
Studio\2017\Enterprise\VC\Tools\MSVC\14.12.25827\include\algorithm(3138):
error : access violation
1> return (pair<_RanIt, _RanIt>(_Pfirst, _Plast));
1> ^
I'm using visual studio enterprise 2017 version 15.5.2 and the intel compiler 64 bit version 17.0.4.210 Build 20170411.
The code is successfully compiled using the default visual studio compiler.
Can't find out what I'm doing wrong.
I've found out that unfortunately visual studio update 15.5.x breaks Intel Compiler 2017 as can be seen in the intel forum where I asked this same question. Hope it will be useful to others too.
Update: using catch 1.9.7 solved this problem.
I am just getting started with using catch and have been unable to get even a simple test project to compile. I am using Visual Studio 2008 and have catch.hpp v1.10.0 (single file version).
I created a simple test project following the catch tutorial. main.cpp is the only file and the code consists of:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("test case 1")
{
REQUIRE(1==2);
}
I get the following errors when I compile:
c:\utilities\catch\catchtest\catchtest\catch.hpp(1333) : warning C4181: qualifier applied to reference type; ignored
c:\utilities\catch\catchtest\catchtest\catch.hpp(1838) : see reference to class template instantiation 'Catch::Internal::Evaluator<T1,T2,Op>' being compiled
with
[
T1=const int &,
T2=const int &,
Op=IsEqualTo
]
c:\utilities\catch\catchtest\catchtest\catch.hpp(1836) : while compiling class template member function 'void Catch::BinaryExpression<LhsT,Op,RhsT>::endExpression(void) const'
with
[
LhsT=const int &,
Op=IsEqualTo,
RhsT=const int &
]
c:\utilities\catch\catchtest\catchtest\main.cpp(8) : see reference to class template instantiation 'Catch::BinaryExpression<LhsT,Op,RhsT>' being compiled
with
[
LhsT=const int &,
Op=IsEqualTo,
RhsT=const int &
]
c:\utilities\catch\catchtest\catchtest\catch.hpp(1333) : error C2529: 'lhs' : reference to reference is illegal
c:\utilities\catch\catchtest\catchtest\catch.hpp(1333) : warning C4181: qualifier applied to reference type; ignored
c:\utilities\catch\catchtest\catchtest\catch.hpp(1333) : error C2529: 'rhs' : reference to reference is illegal
c:\utilities\catch\catchtest\catchtest\catch.hpp(1838) : error C2664: 'Catch::Internal::Evaluator<T1,T2,Op>::evaluate' : cannot convert parameter 1 from 'const int' to 'const int &(&)'
with
[
T1=const int &,
T2=const int &,
Op=IsEqualTo
]
c:\utilities\catch\catchtest\catchtest\catch.hpp(1839) : error C2228: left of '.endExpression' must have class/struct/union
Any assistance would be much appreciated. I haven't been able to find anything in the catch documentation or online and am eager to get started with it.
The latest master of Catch is for modern C++ compiler and the Visual Studio 2008 compiler is pretty old now. There's some switches you can use to downgrade some of the features:
https://github.com/philsquared/Catch/blob/master/docs/configuration.md
Try it with 'CATCH_CONFIG_NO_CPP11' defined before #include'ing catch.
For future reference: support for MSVC 9 was broken by this commit and reverting b6e7c9bd7a160c07c5de894292022687895c17a3 (done on top of it) and then this one is sufficient to fix the problem.
the following code snippet worked in Visual Studio 2005 (with boost 1.34) but it fails to compile in Visual Studio 2015 (with boost 1.62) saying that "error C2672: 'boost::bind': no matching overloaded function found"
Am I missing something here?
Thank you!
typedef boost::shared_ptr< int > SProxySharedPtr;
SProxySharedPtr m_sptr_proxy;
auto a = boost::bind(&SProxySharedPtr::reset, &m_sptr_proxy);
boost::shared_ptr<.>::reset() is an overloaded member function. As a consequence, you have to designate explicitly which overload you want to use:
auto a = boost::bind(static_cast<void(SProxySharedPtr::*)()>(&SProxySharedPtr::reset), &m_sptr_proxy);
I tried to use GCC but saw similar errors. The only way I can get it compile is to subclass boost::shared_ptr as below (but maybe this is not what you are asking for):
typedef boost::shared_ptr<int> SProxySharedPtr;
struct Bar : SProxySharedPtr {
void reset() {
SProxySharedPtr::reset();
}
};
int main()
{
const Bar m_sptr_proxy;
boost::bind(&Bar::reset, &m_sptr_proxy);
}
I have such code:
#include <functional>
struct Foo {
template<typename T>
static void f(int);
};
template<>
void Foo::f<int>(int) {}
int main()
{
std::function<void(int)> func;
//func = static_cast<void (*)(int)>(Foo::f<int>);/works
func = Foo::f<int>;//compilation failure
return 0;
}
VS 2012 and 2013 Preview give compile time error at line:
func = Foo::f<int>;//compilation failure
error C3867: Foo::f: in function call no argument list, use "&Foo::f" to create pointer to member
error C2440: =: can not convert "overloaded-function" into "std::function"
But gcc 4.8.1, clang 3.3 and Intel Compiler 13.1.3 compile
this code with options (-Wall -pedantic -std=c++11) without
any warnings and errors.
So is this compiler bug of C++ compiler of Visual Studio?
Yes, it is a bug, visual studio 2013 rc compiles this without a problem so it looks like they have fixed it.