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.
Related
Function templates are not seen by the auto-vectorization or the auto-parallelizer (/Qpar) engine in VS2013.
For example, this code:
void foo::someFunc(int a)
{
int myArray[1000000];
for (unsigned i = 0; i < 1000000; i++)
{
myArray[i] = i+1;
}
}
seems to be recognized and I get the appropriate output from /Qvec-report:2 and /Qpar-report:2:
foo.cpp
--- Analyzing function: void __cdecl foo::someFunc(int) __ptr64
c:\visual studio 2013\projects\autovectest\autovectest\foo.cpp(18) : info C5001: loop vectorized
c:\visual studio 2013\projects\autovectest\autovectest\foo.cpp(18) : info C5012: loop not parallelized due to reason '1007'
AutoVecTest.vcxproj -> c:\visual studio 2013\Projects\AutoVecTest\x64\Debug\AutoVecTest.dll
But, as soon as I turn someFunc() into a function template:
template <class T>
void foo::someFunc(T a)
{
int myArray[1000000];
for (unsigned i = 0; i < 1000000; i++)
{
myArray[i] = i+1;
}
}
I get nothing from the auto-vectorizer or the auto-parallelizer in the logs:
foo.cpp
AutoVecTest.vcxproj -> c:\visual studio 2013\Projects\AutoVecTest\x64\Debug\AutoVecTest.dll
I am not using /GL as stated in Why would /Qvec-report:2 return nothing ? (MSVC 2012)
As Retired Ninja pointed out, make sure your function template is actually called or instantiated.
Make sure the proper optimization compile flags are enabled. YMMV here, but Visual Studio 2012's Auto-Vectorization Cookbook says /O2 or /O2 /GL will work. Another user discovered /GL did not work for them (Why would /Qvec-report:2 return nothing ? (MSVC 2012)). Using #pragma("gt", on) enabled the auto-vectorizer for me.
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
...
The following simplified code:
#include "Eigen/Core"
template <int n>
Eigen::Matrix<float, 1, n> init_array(float a)
{
Eigen::Matrix<float, 1, n> res;
res.fill(a);
return res;
}
int main()
{
Eigen::Matrix<float, 1, 3> a = init_array<3>(1.0f);
}
leads to the Visual Studio crash:
main.cpp(13): fatal error C1001: An internal error has occurred in the compiler.
(compiler file 'msc1.cpp', line 1325)
To work around this problem, try simplifying or changing the program near the locations listed above.
I downloaded the latest version of Eigen 3.2.5.
I have the Visual Studio Professional 2013 Version 12.0.21005.1.
Does anybody know a workaround?
I was having this same problem. Apparently, you should update your Visual Studio 2013. This worked for me.
This problem was reported to Microsoft and they fix it in the update 2 (current is 5)
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
I'm trying to compile my project with Intel's C++ Compiler but I'm getting many errors like these:
1>..\src\luascript.cpp(5889): error : identifier "__func__" is undefined
1> reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
1> ^
I've compiled this project with MS Visual Studio before and got no warnings or errors but with ICC I get this. Here is the section of code that produces that error
int32_t LuaScriptInterface::luaNetworkMessageAddItem(lua_State* L)
{
// networkMessage:addItem(item)
Item* item = getUserdata<Item>(L, 2);
if (!item) {
reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND)); //This is the line that the error points to
lua_pushnil(L);
return 1;
}
//...
}
The definition reportErrorFunc is:
#define reportErrorFunc(a) reportError(__FUNCTION__, a, true)
There is also:
#ifndef __FUNCTION__
#define __FUNCTION__ __func__
#endif
Please let me know if you need me to post anymore code
I'm on Windows 7 SP1 x64 with MSVC 2013 Ultimate and Intel C++ Studio XE 2013 SP1 U2
Depending on the version of Intel XE the __func__ predeclared identifier may or may not be available. Make sure you use /Qstd=c++11 to enable its availability.
More information is availble at:
https://software.intel.com/en-us/articles/c0x-features-supported-by-intel-c-compiler