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.
Related
While trying to debug an issue with an assert macro I came across this problem. Using __declspec(noinline) on a lambda function in a template class generates a syntax warning in Visual Studio 2017:
error C2760: syntax error: unexpected token '__declspec', expected '{'
This is the failing code:
template<class R>
class test
{
public:
void DoStuff()
{
[]() __declspec(noinline) { }; // syntax error
}
};
int WinMain(void)
{
return 0;
}
If I go to my project settings and switch my platform toolset from v141 (vs2017) to v140 (vs2015) in the general section of the project properties dialog the error goes away .
If I change the class to not be a template class it also compiles correctly:
class test
{
public:
void DoStuff()
{
[]() __declspec(noinline) { }; // compiles fine
}
};
int WinMain(void)
{
return 0;
}
I'm curious why this wouldn't succeed using the v141 platform toolset. Is there some other project setting that could be affecting this?
I was able to fix this by updating Visual Studio 2017 to the latest version (15.9.7). Previously I was running version 15.6.7. Thanks to everyone who looked in and commented! :)
It works in VC++ 2019, so may just be a regression in 2017?
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);
}
The following code fails to compile in VS2015.
struct Foo
{
Foo(int value) { }
};
struct Moo
{
struct
{
Foo foo = 0;
} fooHolder;
};
int main()
{
Moo moo;
}
The following error is shown.
1>c:\xxx\main.cpp(81): error C2512: 'Foo' : no appropriate default constructor available
1> This diagnostic occurred in the compiler generated function 'Moo::<unnamed-type-fooHolder>::(void) restrict(cpu, amp)'
If the unnamed struct is given a name, the code compiles.
struct NamedHolder
{
Foo foo = 0;
} fooHolder;
The code compiled in clang and gcc. http://coliru.stacked-crooked.com/a/3b4ab035a967eed9
Is it rejecting valid code?
This code is perfectly fine and it compiles with VS2015 Update 1 RC (just verified). Maybe you're missing something . The system on which I tested :
Microsoft Visual Studio Community 2015
Version 14.0.24627.00 Update 1 RC
Microsoft .NET Framework
Version 4.6.01040
Installed Version: Community
Visual C++ 2015 RC 00322-20000-00000-AA392
Microsoft Visual C++ 2015 RC
...
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
Consider the following code, which uses a function with variable arguments:
#include <iostream>
// Typedef function type
template<typename... Output>
using Func = void(Output*...);
// Function runner
template<typename... Output>
void run_func(Func<Output...>& func, Output*... output) {
for (int i=0 ; i < 10 ; ++i) {
func(output...);
}
}
void f(double* d) {
*d *= 2;
};
int main() {
double value = 1.0;
run_func(f, &value);
printf("%f\n", value);
}
Compiling this with g++ 4.7.3 works fine, and running produces 1024.0 as expected.
Compiling using icpc 14.0.2 crashes it...
templ.cc(21): internal error: assertion failed: lower_expr: bad kind (shared/cfe/edgcpfe/lower_il.c, line 18582)
run_func(f, &value);
^
Compiling with clang 3.5.0-1 gives the following error message:
templ.cc:21:3: error: no matching function for call to 'run_func'
run_func(f, &value);
^~~~~~~~
templ.cc:9:6: note: candidate template ignored: deduced conflicting types for parameter 'Output' ('double' vs. <double>)
void run_func(Func<Output...>& func, Output*... output) {
^
Is this a bug, or should have g++ not compiled this?
Why is clang deducing these "conflicting" types of double and <double>, is <double> meant to represent an unpacked arglist for example?
Update icpc 14.0.3 does not crash, and the program compiles and runs correctly.
See DPD200244439 at IntelĀ® Composer XE 2013 SP1 Compilers Fixes List
Following the above discussion, it seems that this is indeed a bug in clang.
As pointed out by gha.st, skipping template using and using the native function type directly works:
void run_func(void (&func)(Output*...), Output*... output) {
I have a filed a bug against this here.