I am new to CUDA and the thrust library. I've been looking through a lot of examples and questions regarding my problem, however, I was not able to transfer a solution.
I have a class Cell which should contain a vector of Tree (another class).
This is my Cell.h
#pragma once
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/reduce.h>
#include <thrust/functional.h>
#include "Tree.h"
class Cell {
int idx;
float xmid, ymid;
float dx, dy;
int nTrees;
thrust::host_vector<Tree> trees;
// thrust::device_vector<Tree> trees; <-- this is what I want
public:
Cell();
Cell(int, float, float, float, float, int);
void set(int, float, float, float, float, int);
void add(float, float, float);
void add(float);
void add();
virtual ~Cell();
void print();
void copyToDev();
};
and here is my Tree.h
#pragma once
#include <iostream>
#include <cstdlib>
using namespace std;
class Tree {
float x, y, r;
int idx;
public:
Tree();
Tree(float, float, float, int);
void set(float, float, float, int);
virtual ~Tree();
void print();
};
The classes are both implemented in a file with extension .cu. In my main.cu I now want to initialize a Cell C. Given the code above, I am able to compile the code (using Visual Studio 2013 which I haven't used before, so that might be another problem for me).
However, if I'm not mistaken, using host_vector I do not make use of my GPU. What I want is to use device_vector instead.
But if I compile the code with device_vector instead of host_vector I get the following error
1>------ Build started: Project: WTM, Configuration: Debug Win32 ------
1> Cell.cu
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\detail\error_category.inl(102): warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h(168) : see declaration of 'strerror'
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\cuda\detail\bulk\detail\pointer_traits.hpp(55): warning C4800: 'unsigned int' : forcing value to bool 'true' or 'false' (performance warning)
1>c:\users\thomas\documents\visual studio 2013\projects\wtm\wtm\cell.cu(40): warning C4018: '<' : signed/unsigned mismatch
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\cuda\detail\for_each.inl(84): error C2027: use of undefined type 'thrust::detail::STATIC_ASSERTION_FAILURE<false>'
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\for_each.inl(71) : see reference to function template instantiation 'RandomAccessIterator thrust::system::cuda::detail::for_each_n<thrust::system::cuda::detail::tag,InputIterator,Size,UnaryFunction>(thrust::system::cuda::detail::execution_policy<thrust::system::cuda::detail::tag> &,RandomAccessIterator,Size,UnaryFunction)' being compiled
1> with
1> [
1> RandomAccessIterator=thrust::device_ptr<Tree>
1> , InputIterator=thrust::device_ptr<Tree>
1> , Size=int
1> , UnaryFunction=thrust::detail::allocator_traits_detail::gozer
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\allocator\destroy_range.inl(136) : see reference to function template instantiation 'InputIterator thrust::for_each_n<DerivedPolicy,Pointer,Size,thrust::detail::allocator_traits_detail::gozer>(const thrust::detail::execution_policy_base<DerivedPolicy> &,InputIterator,Size,UnaryFunction)' being compiled
1> with
1> [
1> InputIterator=thrust::device_ptr<Tree>
1> , DerivedPolicy=thrust::system::cuda::detail::tag
1> , Pointer=thrust::device_ptr<Tree>
1> , Size=int
1> , UnaryFunction=thrust::detail::allocator_traits_detail::gozer
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\allocator\destroy_range.inl(157) : see reference to function template instantiation 'void thrust::detail::allocator_traits_detail::destroy_range<Allocator,Pointer,Size>(Allocator &,Pointer,Size)' being compiled
1> with
1> [
1> Allocator=thrust::device_malloc_allocator<Tree>
1> , Pointer=thrust::device_ptr<Tree>
1> , Size=int
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\contiguous_storage.inl(256) : see reference to function template instantiation 'void thrust::detail::destroy_range<thrust::device_malloc_allocator<T>,Base,int>(Allocator &,Pointer,Size)' being compiled
1> with
1> [
1> T=Tree
1> , Base=thrust::device_ptr<Tree>
1> , Allocator=thrust::device_malloc_allocator<Tree>
1> , Pointer=thrust::device_ptr<Tree>
1> , Size=int
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\contiguous_storage.inl(255) : while compiling class template member function 'void thrust::detail::contiguous_storage<T,Alloc>::destroy(thrust::detail::normal_iterator<thrust::device_ptr<T>>,thrust::detail::normal_iterator<thrust::device_ptr<T>>)'
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\vector_base.inl(474) : see reference to function template instantiation 'void thrust::detail::contiguous_storage<T,Alloc>::destroy(thrust::detail::normal_iterator<thrust::device_ptr<T>>,thrust::detail::normal_iterator<thrust::device_ptr<T>>)' being compiled
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\vector_base.inl(44) : while compiling class template member function 'thrust::detail::vector_base<T,Alloc>::vector_base(void)'
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\device_vector.h(67) : see reference to function template instantiation 'thrust::detail::vector_base<T,Alloc>::vector_base(void)' being compiled
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\device_vector.h(54) : see reference to class template instantiation 'thrust::detail::vector_base<T,Alloc>' being compiled
1> with
1> [
1> T=Tree
1> , Alloc=thrust::device_malloc_allocator<Tree>
1> ]
1> c:\users\thomas\documents\visual studio 2013\projects\wtm\wtm\cell.h(27) : see reference to class template instantiation 'thrust::device_vector<Tree,thrust::device_malloc_allocator<T>>' being compiled
1> with
1> [
1> T=Tree
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'm really not sure how to approach this. Maybe someone could guide me through this problem. Thank you in advance!
EDIT
As pointed out, I forgot the main, this is the code below, aso in a .cu file
#include "Cell.h"
int main(void)
{
Cell D(0, 0.5, 0.5, 1, 1);
D.print();
return 0;
}
reduced to the minimal part. If further information are missing, just let me know.
If you look into the file with the definition of thrust::system::cuda::detail::for_each_n like the error message is suggesting, you will find the following comment (link to github):
// we're attempting to launch a kernel, assert we're compiling with nvcc
// ========================================================================
// X Note to the user: If you've found this line due to a compiler error, X
// X you need to compile your code using nvcc, rather than g++ or cl.exe X
// ========================================================================
Make sure you are really using nvcc to compile your program, and not merely trying to include thrust in a normal c++ project which gets compiled with visual studio's cl.
Related
The following piece of code throwing warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning). Moving #define true and false after string and map header inclusions not throwing this warning. This is a sample code and in the actual code #define true and false are defined in one of our product library header files. So I want to understand why this warning is thrown by visual studio and why it is going away when #define moved after string and map header inclusions.
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
#include <string>
#include <map>
int main()
{
std::map<std::string, std::string> pair;
pair.insert(std::make_pair("Key1", "Value1"));
return 0;
}
Full details of warning:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\utility(144): warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree(1801) : see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool>::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,int,void>(_Other1 &&,_Other2 &&)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::string
1> , _Other1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,std::string>>>>
1> , _Other2=int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree(1801) : see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool>::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,int,void>(_Other1 &&,_Other2 &&)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::string
1> , _Other1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,std::string>>>>
1> , _Other2=int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree(1160) : see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Insert_nohint<std::pair<const _Kty,_Ty>&,std::_Tree_node<std::pair<const _Kty,_Ty>,void *>*>(bool,_Valty,_Nodety)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::string
1> , _Pr=std::less<std::string>
1> , _Alloc=std::allocator<std::pair<const std::string,std::string>>
1> , _Valty=std::pair<const std::string,std::string> &
1> , _Nodety=std::_Tree_node<std::pair<const std::string,std::string>,void *> *
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree(1160) : see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Insert_nohint<std::pair<const _Kty,_Ty>&,std::_Tree_node<std::pair<const _Kty,_Ty>,void *>*>(bool,_Valty,_Nodety)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::string
1> , _Pr=std::less<std::string>
1> , _Alloc=std::allocator<std::pair<const std::string,std::string>>
1> , _Valty=std::pair<const std::string,std::string> &
1> , _Nodety=std::_Tree_node<std::pair<const std::string,std::string>,void *> *
1> ]
1> Source.cpp(41) : see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::insert<std::pair<const char *,const char *>>(_Valty &&)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::string
1> , _Pr=std::less<std::string>
1> , _Alloc=std::allocator<std::pair<const std::string,std::string>>
1> , _Valty=std::pair<const char *,const char *>
1> ]
1> Source.cpp(41) : see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::insert<std::pair<const char *,const char *>>(_Valty &&)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::string
1> , _Pr=std::less<std::string>
1> , _Alloc=std::allocator<std::pair<const std::string,std::string>>
1> , _Valty=std::pair<const char *,const char *>
1> ]
I'm not condoning your macros, but the answer to your question is that your check isn't nearly complete. In C++, true and false are keywords, not macros like in C's stdbool.h header. So checking if they aren't defined is always going to be true. If you want to C++-proof those macros, the condition should be something like this:
#if !defined(__cplusplus) && !defined(true)
#define true 1
#endif
__cplusplus is a standard macro that is defined by the implementation when a TU is compiled as C++. The added check will prevent messing up with those keywords, and breaking any standard library headers that use them.
You say
the #defines are declared in one of our product header files(not written by me)
Then, you should tell the guy who wrote this code to modify it. It's probably pure C code (where true/false are not defined) and he decided to declare them, why not, but then, I thing the good strategy would be for him not to use the standard names true/false that end up confliciting with standard ones in your case. He should use MyTrue/MyFalse (or whetever name that would make sense in the context).
So just (or ask him to) go in the code where true/false was defined, replace all occurence of "true" by "MyTrue", all "false" by "MyFalse", the code behaviour should not be impacted. The conflict will go away as well as the warning!
First of all, I use boost library, and if it changes anything, the code is compiled on a Windows Machine.
The code itself contains a lot more of function acting upon matrices but only this one triggers the error.
Well, I am trying to transform matrix like :
{001
100
010}
To something like :
{1
3
2}
But strangely I can't compile my code and I can't find the error so I would be glad if anyone could help me.
Below the code :
using namespace boost::numeric::ublas;
typedef matrix <float, row_major, unbounded_array<float>> MATRIXf;
MATRIXf matrix_to_class (const MATRIXf inputM)
{
MATRIXf output;
for (std::size_t line = 0; line < inputM.size1(); line++)
{
for (std::size_t column = 0; column < inputM.size2(); column++)
{
if (column == 1)
{
output.insert_element(line,0.0,column);
}
}
}
return output;
}
Here is the error code:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): error C4996: 'std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): note: see declaration of 'std::copy::_Unchecked_iterators::_Deprecate'
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(204): note: see reference to function template instantiation '_OutIt *std::copy<float*,float*>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=float *,
1> _InIt=float *
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(201): note: while compiling class template member function 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>> &boost::numeric::ublas::unbounded_array<T,std::allocator<T>>::operator =(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)'
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(310): note: see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>> &boost::numeric::ublas::unbounded_array<T,std::allocator<T>>::operator =(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)' being compiled
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(102): note: see reference to class template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>' being compiled
1> with
1> [
1> T=float
1> ]
1> g:\c++ python\travail\visualstudio\visualstudio\guigui\neural net\neural net\utils.hpp(21): note: see reference to class template instantiation 'boost::numeric::ublas::matrix<float,boost::numeric::ublas::row_major,boost::numeric::ublas::unbounded_array<float,std::allocator<T>>>' being compiled
1> with
1> [
1> T=float
1> ]
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory(102): error C4996: 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate': Call to 'std::uninitialized_copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory(102): note: see declaration of 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate'
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(94): note: see reference to function template instantiation '_FwdIt *std::uninitialized_copy<const float*,float*>(_InIt,_InIt,_FwdIt)' being compiled
1> with
1> [
1> _FwdIt=float *,
1> _InIt=const float *
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(89): note: while compiling class template member function 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>::unbounded_array(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)'
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(162): note: see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>::unbounded_array(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)' being compiled
1> with
1> [
1> T=float
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Trying to locate the error brings me to the function above.
Thanks in advance.
Near the end of the first error message is the text, "To disable this warning, use -D_SCL_SECURE_NO_WARNINGS". https://msdn.microsoft.com/en-us/library/ttcz0bys.aspx has a more detailed discussion of warnings and checked iterators. In essence, Microsoft created "safe" mutations of Standard C++ functions to help developers avoid invalid iterator usage. The error message suggests that you define _SCL_SECURE_NO_WARNINGS. This can be done in the project properties C/C++/Preprocessor/Preprocessor Definitions. In a project I worked on in the past, we disabled all the "safe" versions of the functions because of the performance hit.
You may be interested in reading the above Microsoft page for more information about the checked iterator topic.
I have portable Linux/Windows application. I use boost Signal2 library to implement events system.
Microsoft has built-in memory leak detection feature in Visual C++. You have to include some code at the very beginning of any source file to replace normal malloc and new with theirs debug versions, and these debug memory management functions generate memory leak report when application exits. The code follows:
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif
When I include this code, the program stops to compile. Error messages are not understandable and don't even point to source file where the error occurred (they point to boost headers, but not to my program headers or code files). Error report follows:
D:\src\open-source\boost\boost/variant/detail/initializer.hpp(122): error C2061: syntax error : identifier 'dest'
1> D:\src\open-source\boost\boost/variant/detail/initializer.hpp(116) : while compiling class template member function 'int boost::detail::variant::make_initializer_node::apply<T1,T2>::initializer_node::initialize(void *,boost::shared_ptr<void> &&)'
1> with
1> [
1> T1=boost::mpl::pair<boost::detail::variant::initializer_root,boost::mpl::int_<0>>
1> , T2=boost::mpl::l_iter<boost::mpl::list2<boost::shared_ptr<void>,boost::signals2::detail::foreign_void_shared_ptr>>
1> ]
1> D:\src\open-source\boost\boost/variant/variant.hpp(1559) : see reference to function template instantiation 'int boost::detail::variant::make_initializer_node::apply<T1,T2>::initializer_node::initialize(void *,boost::shared_ptr<void> &&)' being compiled
1> with
1> [
1> T1=boost::mpl::pair<boost::detail::variant::initializer_root,boost::mpl::int_<0>>
1> , T2=boost::mpl::l_iter<boost::mpl::list2<boost::shared_ptr<void>,boost::signals2::detail::foreign_void_shared_ptr>>
1> ]
1> D:\src\open-source\boost\boost/variant/detail/initializer.hpp(76) : see reference to class template instantiation 'boost::detail::variant::make_initializer_node::apply<T1,T2>::initializer_node' being compiled
1> with
1> [
1> T1=boost::mpl::pair<boost::detail::variant::initializer_root,boost::mpl::int_<0>>
1> , T2=boost::mpl::l_iter<boost::mpl::list2<boost::shared_ptr<void>,boost::signals2::detail::foreign_void_shared_ptr>>
1> ]
1> D:\src\open-source\boost\boost/variant/variant.hpp(1339) : see reference to class template instantiation 'boost::detail::variant::make_initializer_node::apply<T1,T2>::initializer_node' being compiled
1> with
1> [
1> T1=boost::mpl::pair<boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root,boost::mpl::int_<0>>,boost::mpl::l_iter<boost::mpl::list2<boost::shared_ptr<void>,boost::signals2::detail::foreign_void_shared_ptr>>>::initializer_node,boost::mpl::int_<1>>
1> , T2=boost::mpl::l_iter<boost::mpl::list1<boost::signals2::detail::foreign_void_shared_ptr>>
1> ]
1> D:\src\open-source\boost\boost/variant/variant.hpp(1555) : see reference to class template instantiation 'boost::variant<boost::shared_ptr<void>,boost::signals2::detail::foreign_void_shared_ptr,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_>::initializer' being compiled
1> D:\src\open-source\boost\boost/variant/variant.hpp(1732) : see reference to function template instantiation 'void boost::variant<boost::shared_ptr<void>,boost::signals2::detail::foreign_void_shared_ptr,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_>::convert_construct<boost::shared_ptr<void>>(T &&,int,boost::mpl::false_)' being compiled
1> with
1> [
1> T=boost::shared_ptr<void>
1> ]
1> D:\src\open-source\boost\boost/variant/variant.hpp(1732) : see reference to function template instantiation 'void boost::variant<boost::shared_ptr<void>,boost::signals2::detail::foreign_void_shared_ptr,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_>::convert_construct<boost::shared_ptr<void>>(T &&,int,boost::mpl::false_)' being compiled
1> with
1> [
1> T=boost::shared_ptr<void>
1> ]
1> D:\src\open-source\boost\boost/signals2/slot_base.hpp(49) : see reference to function template instantiation 'boost::variant<boost::shared_ptr<void>,boost::signals2::detail::foreign_void_shared_ptr,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_>::variant<boost::shared_ptr<void>>(T &&,void *,void *)' being compiled
1> with
1> [
1> T=boost::shared_ptr<void>
1> ]
1> D:\src\open-source\boost\boost/signals2/slot_base.hpp(49) : see reference to function template instantiation 'boost::variant<boost::shared_ptr<void>,boost::signals2::detail::foreign_void_shared_ptr,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_>::variant<boost::shared_ptr<void>>(T &&,void *,void *)' being compiled
1> with
1> [
1> T=boost::shared_ptr<void>
1> ]
Is there any workaround to use boost and still use memory leak detection on Windows?
I am using VS2012 and I have problem with following example:
#include <chrono>
#include <thread>
int main()
{
// doesn't compile and I don't understand why:
std::this_thread::sleep_for(std::chrono::duration<double>(0.1));
// I can use this but still I would like to know the reason:
std::this_thread::sleep_for(std::chrono::duration<long long, std::milli>(100));
return 0;
}
Both duration should be valid. And it is possible to use them in different context.
Compile error:
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(749): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'const std::chrono::duration<_Rep>' (or there is no acceptable conversion)
1> with
1> [
1> _Rep=double
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(166): could be 'std::chrono::duration<_Rep,_Period> &std::chrono::duration<_Rep,_Period>::operator +=(const std::chrono::duration<_Rep,_Period> &)'
1> with
1> [
1> _Rep=__int64,
1> _Period=std::nano
1> ]
1> while trying to match the argument list '(std::chrono::nanoseconds, const std::chrono::duration<_Rep>)'
1> with
1> [
1> _Rep=double
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(164) : see reference to function template instantiation 'xtime std::_To_xtime<double,std::ratio<_Nx>>(const std::chrono::duration<_Rep> &)' being compiled
1> with
1> [
1> _Nx=0x01,
1> _Rep=double
1> ]
1> i:\prog\.c++\test2\test2\source.cpp(13) : see reference to function template instantiation 'void std::this_thread::sleep_for<double,std::ratio<_Nx>>(const std::chrono::duration<_Rep> &)' being compiled
1> with
1> [
1> _Nx=0x01,
1> _Rep=double
1> ]
1>
1>Build FAILED.
Any help appreciated.
It is VS2012 compiler issue. Not 100% sure if it is this one (thx Praetorian). But it is possible to compile without problem with gcc 4.9.2. I should've think about trying it before I ask.
I am having trouble pushing an array to the stack. I thought this was pretty straightforward, but I've already spent too much time trying to figure this one out.
I expected to be able to push arrays just as I push ints or floats, but that is not happening.
The push command is giving me the issue. Here is my code:
#include <iostream>
#include <stack>
struct Matrix4x4
{
float data[16];
};
int main(int argc, char **argv)
{
// My original code
typedef std::stack<float[16]> myStack;
myStack modelViewStack;
myStack projectionStack;
float testMat[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
modelViewStack.push(testMat); // THIS LINE GIVES ME ERRORS
////Stack initialization - This is thokra's solution
////typedef std::stack<std::vector<float[16]>> myStack;
//typedef std::stack<Matrix4x4> myStack;
//myStack modelViewStack;
//myStack projectionStack;
//
//Matrix4x4 m = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
////std::vector<float> testMat2(testMat, testMat + sizeof(testMat) / sizeof(float));
//modelViewStack.push(m);
//for(int i = 0; i<16 ; i++)
//{
// std::cout << "m data: " << m.data[i] << std::endl;
//}
//system("pause");
return 0;
}
Thanks for your help!
Here are the errors. I can't decipher them. Maybe an explanation of how to read these would be helpful too.
1>------ Build started: Project: opengl4_4, Configuration: Release Win32 ------
1> main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(606): error C2075: 'Target of operator new()' : array initialization needs curly braces
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty (*),const _Ty (&))'
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty (*),const _Ty (&))' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\deque(925) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::allocator<float [16]>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\stack(21) : see reference to class template instantiation 'std::deque<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> main.cpp(14) : see reference to class template instantiation 'std::stack<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(606): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Just encapsulate the data store holding the elements of the matrix in a suitable type:
#include <stack>
struct Matrix4x4
{
float data[16];
};
int main()
{
typedef std::stack<Matrix4x4> myStack;
myStack modelViewStack;
Matrix4x4 m = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
modelViewStack.push(m);
return 0;
}
More to the point: std::stack::push will internally call push_back on the std::deque that's used as the container when you don't change the default. Essentially, when trying to construct a new element at the end of the deque the container tries to place the new element at the address which currently marks the end of the container with a placement-new. For instance, g++ implements it as follows:
template<typename _Up, typename... _Args>
void
construct(_Up* __p, _Args&&... __args)
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
This effectively boils down to:
::new((void *)__p) float[16](_args); // where __p is a pointer to float[16] and _args is testMat
Trying to copy- or move-initialize a C-array is simply not legal. Even if construction succeeded for some reason, the container would try to call the destructor on an element of type float[16] when going out of scope. One can easily see, a destructor ~T[n] does not exist.
In C++11, you can push a std::array<float,16> instead of defining an additional type.