Search Results

Search found 397 results on 16 pages for 'c 0x'.

Page 7/16 | < Previous Page | 3 4 5 6 7 8 9 10 11 12 13 14  | Next Page >

  • How to declare a function that accepts a lambda?

    - by Andreas Bonini
    I read on the internet many tutorials that explained how to use lambdas with the standard library (such as std::find), and they all were very interesting, but I couldn't find any that explained how I can use a lambda for my own functions. For example: int main() { int test = 5; LambdaTest([&](int a) { test += a; }); return EXIT_SUCCESS; } How should I declare LambdaTest? What's the type of its first argument? And then, how can I call the anonymous function passing to it - for example - "10" as its argument?

    Read the article

  • Can any function be a deleted-function?

    - by Caspin
    The working draft explicitly calls out that defaulted-functions must be special member functions (eg copy-constructor, default-constructor, etc). Which makes perfect sense. However, I don't see any such restriction on deleted-functions. Is that right? Or in other words are these three examples valid c++0? struct Foo { // 1 int bar( int ) = delete; }; // 2 int baz( int ) = delete; template< typename T > int boo( T t ); // 3 template<> int boo<int>(int t) = delete;

    Read the article

  • decltype, result_of, or typeof?

    - by Neil G
    I have: class A { public: B toCPD() const; And: template<typename T> class Ev { public: typedef result_of(T::toCPD()) D; After instantiating Ev<A>, the compiler says: meta.h:12: error: 'T::toCPD' is not a type neither decltype nor typeof work either.

    Read the article

  • Fill container with template parameters

    - by phlipsy
    I want to fill the template parameters passed to a variadic template into an array with fixed length. For that purpose I wrote the following helper function templates template<typename ForwardIterator, typename T> void fill(ForwardIterator i) { } template<typename ForwardIterator, typename T, T head, T... tail> void fill(ForwardIterator i) { *i = head; fill<ForwardIterator, T, tail...>(++i); } the following class template template<typename T, T... args> struct params_to_array; template<typename T, T last> struct params_to_array<T, last> { static const std::size_t SIZE = 1; typedef std::array<T, SIZE> array_type; static const array_type params; private: void init_params() { array_type result; fill<typename array_type::iterator, T, head, tail...>(result.begin()); return result; } }; template<typename T, T head, T... tail> struct params_to_array<T, head, tail...> { static const std::size_t SIZE = params_to_array<T, tail...>::SIZE + 1; typedef std::array<T, SIZE> array_type; static const array_type params; private: void init_params() { array_type result; fill<typename array_type::iterator, T, last>(result.begin()); return result; } }; and initialized the static constants via template<typename T, T last> const typename param_to_array<T, last>::array_type param_to_array<T, last>::params = param_to_array<T, last>::init_params(); and template<typename T, T head, T... tail> const typename param_to_array<T, head, tail...>::array_type param_to_array<T, head, tail...>::params = param_to_array<T, head, tail...>::init_params(); Now the array param_to_array<int, 1, 3, 4>::params is a std::array<int, 3> and contains the values 1, 3 and 4. I think there must be a simpler way to achieve this behavior. Any suggestions? Edit: As Noah Roberts suggested in his answer I modified my program like the following: I wrote a new struct counting the elements in a parameter list: template<typename T, T... args> struct count; template<typename T, T head, T... tail> struct count<T, head, tail...> { static const std::size_t value = count<T, tail...>::value + 1; }; template<typename T, T last> stuct count<T, last> { static const std::size_t value = 1; }; and wrote the following function template<typename T, T... args> std::array<T, count<T, args...>::value> params_to_array() { std::array<T, count<T, args...>::value> result; fill<typename std::array<T, count<T, args...>::value>::iterator, T, args...>(result.begin()); return result; } Now I get with params_to_array<int, 10, 20, 30>() a std::array<int, 3> with the content 10, 20 and 30. Any further suggestions?

    Read the article

  • How do I compile variadic templates conditionally?

    - by FredOverflow
    Is there a macro that tells me whether or not my compiler supports variadic templates? #ifdef VARIADIC_TEMPLATES_AVAILABLE template<typename... Args> void coolstuff(Args&&... args); #else ??? #endif If they are not supported, I guess I would simulate them with a bunch of overloads. Any better ideas? Maybe there are preprocessor libraries that can ease the job?

    Read the article

  • Decomposing a rotation matrix

    - by DeadMG
    I have a rotation matrix. How can I get the rotation around a specified axis contained within this matrix? Edit: It's a 3D matrix (4x4), and I want to know how far around a predetermined (not contained) axis the matrix rotates. I can already decompose the matrix but D3DX will only give the entire matrix as one rotation around one axis, whereas I need to split the matrix up into angle of rotation around an already-known axis, and the rest. Sample code and brief problem description: D3DXMATRIX CameraRotationMatrix; D3DXVECTOR3 CameraPosition; //D3DXVECTOR3 CameraRotation; inline D3DXMATRIX GetRotationMatrix() { return CameraRotationMatrix; } inline void TranslateCamera(float x, float y, float z) { D3DXVECTOR3 rvec, vec(x, y, z); #pragma warning(disable : 4238) D3DXVec3TransformNormal(&rvec, &vec, &GetRotationMatrix()); #pragma warning(default : 4238) CameraPosition += rvec; RecomputeVPMatrix(); } inline void RotateCamera(float x, float y, float z) { D3DXVECTOR3 RotationRequested(x, y, z); D3DXVECTOR3 XAxis, YAxis, ZAxis; D3DXMATRIX rotationx, rotationy, rotationz; XAxis = D3DXVECTOR3(1, 0, 0); YAxis = D3DXVECTOR3(0, 1, 0); ZAxis = D3DXVECTOR3(0, 0, 1); #pragma warning(disable : 4238) D3DXVec3TransformNormal(&XAxis, &XAxis, &GetRotationMatrix()); D3DXVec3TransformNormal(&YAxis, &YAxis, &GetRotationMatrix()); D3DXVec3TransformNormal(&ZAxis, &ZAxis, &GetRotationMatrix()); #pragma warning(default : 4238) D3DXMatrixIdentity(&rotationx); D3DXMatrixIdentity(&rotationy); D3DXMatrixIdentity(&rotationz); D3DXMatrixRotationAxis(&rotationx, &XAxis, RotationRequested.x); D3DXMatrixRotationAxis(&rotationy, &YAxis, RotationRequested.y); D3DXMatrixRotationAxis(&rotationz, &ZAxis, RotationRequested.z); CameraRotationMatrix *= rotationz; CameraRotationMatrix *= rotationy; CameraRotationMatrix *= rotationx; RecomputeVPMatrix(); } inline void RecomputeVPMatrix() { D3DXMATRIX ProjectionMatrix; D3DXMatrixPerspectiveFovLH( &ProjectionMatrix, FoV, (float)D3DDeviceParameters.BackBufferWidth / (float)D3DDeviceParameters.BackBufferHeight, FarPlane, NearPlane ); D3DXVECTOR3 CamLookAt; D3DXVECTOR3 CamUpVec; #pragma warning(disable : 4238) D3DXVec3TransformNormal(&CamLookAt, &D3DXVECTOR3(1, 0, 0), &GetRotationMatrix()); D3DXVec3TransformNormal(&CamUpVec, &D3DXVECTOR3(0, 1, 0), &GetRotationMatrix()); #pragma warning(default : 4238) D3DXMATRIX ViewMatrix; #pragma warning(disable : 4238) D3DXMatrixLookAtLH(&ViewMatrix, &CameraPosition, &(CamLookAt + CameraPosition), &CamUpVec); #pragma warning(default : 4238) ViewProjectionMatrix = ViewMatrix * ProjectionMatrix; D3DVIEWPORT9 vp = { 0, 0, D3DDeviceParameters.BackBufferWidth, D3DDeviceParameters.BackBufferHeight, 0, 1 }; D3DDev->SetViewport(&vp); } Effectively, after a certain time, when RotateCamera is called, it begins to rotate in the relative X axis- even though constant zero is passed in for that request when responding to mouse input, so I know that when moving the mouse, the camera should not roll at all. I tried spamming 0,0,0 requests and saw no change (one per frame at 1500 frames per second), so I'm fairly sure that I'm not seeing FP error or matrix accumulation error. I tried writing a RotateCameraYZ function and stripping all X-axis from the function. I've spent several days trying to discover why this is the case, and eventually decided on just hacking around it. Just for reference, I've seen some diagrams on Wikipedia, and I actually have a relatively strange axis layout, which is Y axis up, but X axis forwards and Z axis right, so Y axis yaw, Z axis pitch, X axis roll.

    Read the article

  • Will C++1x support __stdcall or extern "C" capture-nothing lambdas?

    - by Daniel Trebbien
    Yesterday I was thinking about whether it would be possible to use the convenience of C++1x lambda functions to write callbacks for Windows API functions. For example, what if I wanted to use a lambda as an EnumChildProc with EnumChildWindows? Something like: EnumChildWindows(hTrayWnd, CALLBACK [](HWND hWnd, LPARAM lParam) { // ... return static_cast<BOOL>(TRUE); // continue enumerating }, reinterpret_cast<LPARAM>(&myData)); Another use would be to write extern "C" callbacks for C routines. E.g.: my_class *pRes = static_cast<my_class*>(bsearch(&key, myClassObjectsArr, myClassObjectsArr_size, sizeof(my_class), extern "C" [](const void *pV1, const void *pV2) { const my_class& o1 = *static_cast<const my_class*>(pV1); const my_class& o2 = *static_cast<const my_class*>(pV2); int res; // ... return res; })); Is this possible?

    Read the article

  • Building a world matrix

    - by DeadMG
    When building a world projection matrix from scale, rotate, translate matrices, then the translation matrix must be the last in the process, right? Else you'll be scaling or rotating your translations. Do scale and rotate need to go in a specific order? Right now I've got std::for_each(objects.begin(), objects.end(), [&, this](D3D93DObject* ptr) { D3DXMATRIX WVP; D3DXMATRIX translation, rotationX, rotationY, rotationZ, scale; D3DXMatrixTranslation(&translation, ptr->position.x, ptr->position.y, ptr->position.z); D3DXMatrixRotationX(&rotationX, ptr->rotation.x); D3DXMatrixRotationY(&rotationY, ptr->rotation.y); D3DXMatrixRotationZ(&rotationZ, ptr->rotation.z); D3DXMatrixScaling(&translation, ptr->scale.x, ptr->scale.y, ptr->scale.z); WVP = rotationX * rotationY * rotationZ * scale * translation * ViewProjectionMatrix; });

    Read the article

  • Visual C++ 2010, rvalue reference bug?

    - by Sergey Shandar
    Is it a bug in Visual C++ 2010 or right behaviour? template<class T> T f(T const &r) { return r; } template<class T> T f(T &&r) { static_assert(false, "no way"); return r; } int main() { int y = 4; f(y); } I thought, the function f(T &&) should never be called but it's called with T = int &. The output: main.cpp(10): error C2338: no way main.cpp(17) : see reference to function template instantiation 'T f<int&>(T)' being compiled with [ T=int & ] Update 1 Do you know any C++x0 compiler as a reference? I've tried comeau online test-drive but could not compile r-value reference. Update 2 Workaround (using SFINAE): #include <boost/utility/enable_if.hpp> #include <boost/type_traits/is_reference.hpp> template<class T> T f(T &r) { return r; } template<class T> typename ::boost::disable_if< ::boost::is_reference<T>, T>::type f(T &&r) { static_assert(false, "no way"); return r; } int main() { int y = 4; f(y); // f(5); // generates "no way" error, as expected. }

    Read the article

  • rvalues and temporary objects in the FCD

    - by FredOverflow
    It took me quite some time to understand the difference between an rvalue and a temporary object. But now the final committee draft states on page 75: An rvalue [...] is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object. I can't believe my eyes. This must be an error, right?

    Read the article

  • What elegant method callback design should be used ?

    - by ereOn
    Hi, I'm surprised this question wasn't asked before on SO (well, at least I couldn't find it). Have you ever designed a method-callback pattern (something like a "pointer" to a class method) in C++ and, if so, how did you do it ? I know a method is just a regular function with some hidden this parameter to serve as a context and I have a pretty simple design in mind. However, since things are often more complex than they seem to, I wonder how our C++ gurus would implement this, preferably in an elegant and standard way. All suggestions are welcome !

    Read the article

  • Allocation Target of std::aligned_storage (stack or heap?)

    - by Zenikoder
    I've been trying to get my head around the TR1 addition known as aligned_storage. Whilst reading the following documents N2165, N3190 and N2140 I can't for the life of me see a statement where it clearly describes stack or heap nature of the memory being used. I've had a look at the implementation provided by msvc2010, boost and gcc they all provide a stack based solution centered around the use of a union. In short: Is the memory type (stack or heap) used by aligned_storage implementation defined or is it always meant to be stack based? and, What the is the specific document that defines/determines that?

    Read the article

  • Separate one-off code paths

    - by DeadMG
    I'm implementing an application with different code paths that shall be chosen once at startup and then fixed forevermore for that execution- for example, choosing D3D11 or D3D9 rendering path. Obviously I don't want to duplicate all my other code. Is run-time inheritance (no virtual inheritance) a fair solution? I don't want to waste a bunch of performance making virtual lookups when the type was fixed long ago. Not just that, but it makes me nervous that the functions can't be inlined and whether or not it affects RVO and NRVO and such. Am I just being over-concerned about this?

    Read the article

  • variadic constructors

    - by FredOverflow
    Are variadic constructors supposed to hide the implicitly generated ones, i.e. the default constructor and the copy constructor? struct Foo { template<typename... Args> Foo(Args&&... x) { std::cout << "inside the variadic constructor\n"; } }; int main() { Foo a; Foo b(a); } Somehow I was expecting this to print nothing after reading this answer, but it prints inside the variadic constructor twice on g++ 4.5.0 :( Is this behavior correct?

    Read the article

  • C++ template overloading - wrong function called

    - by DeadMG
    template<typename T> T* Push(T* ptr); template<typename T> T* Push(T& ref); template<typename T, typename T1> T* Push(T1&& ref); I have int i = 0; Push<int>(i); But the compiler calls it ambiguous. How is that ambiguous? The second function is clearly the preferred match since it's more specialized. Especially since the T1&& won't bind to an lvalue unless I explicitly forward/move it. Sorry - i is an int. Otherwise, the question would make no sense, and I thought people would infer it since it's normally the loop iterator.

    Read the article

  • C++ Types Impossible to Name

    - by Kirakun
    While reading Wikipedia's page on decltype, I was curious about the statement, Its [decltype's] primary intended use is in generic programming, where it is often difficult, or even impossible, to name types that depend on template parameters. While I can understand the difficulty part of that statement, what is an example where there is a need to name a type that cannot be named under C++03? EDIT: My point is that since everything in C++ has a declaration of types. Why would there ever be a case where it is impossible to name a type? Furthermore, aren't trait classes designed to yield type informations? Could trait classes be an alternative to decltype?

    Read the article

  • Nested bind expressions

    - by user328543
    This is a followup question to my previous question. #include <functional> int foo(void) {return 2;} class bar { public: int operator() (void) {return 3;}; int something(int a) {return a;}; }; template <class C> auto func(C&& c) -> decltype(c()) { return c(); } template <class C> int doit(C&& c) { return c();} template <class C> void func_wrapper(C&& c) { func( std::bind(doit<C>, std::forward<C>(c)) ); } int main(int argc, char* argv[]) { // call with a function pointer func(foo); func_wrapper(foo); // error // call with a member function bar b; func(b); func_wrapper(b); // call with a bind expression func(std::bind(&bar::something, b, 42)); func_wrapper(std::bind(&bar::something, b, 42)); // error // call with a lambda expression func( [](void)->int {return 42;} ); func_wrapper( [](void)->int {return 42;} ); return 0; } I'm getting a compile errors deep in the C++ headers: functional:1137: error: invalid initialization of reference of type ‘int (&)()’ from expression of type ‘int (*)()’ functional:1137: error: conversion from ‘int’ to non-scalar type ‘std::_Bind(bar, int)’ requested func_wrapper(foo) is supposed to execute func(doit(foo)). In the real code it packages the function for a thread to execute. func would the function executed by the other thread, doit sits in between to check for unhandled exceptions and to clean up. But the additional bind in func_wrapper messes things up...

    Read the article

  • Performance issue between builds

    - by DeadMG
    I've been developing a small indie game in my spare time and have run across an inexplicable issue. Some builds of the game will randomly run several hundred frames per second slower than other builds. For example, when rendering some text and no 3D scene, I can achieve 1800FPS on my own hardware. Add one 3D sphere (10k verts, pixel shaded), achieve 1700 FPS. Add two more spheres, achieve 800 FPS. Remove all spheres, achieve 1100FPS- even though the code now renders the same scene as I previously achieved at 1800FPS, which is just the FPS counter being rendered. I've tried rebuilding and cleaning the project and rebooting the compiler. This is in Release mode and I turned on all the optimizations I could find. Any suggestions as to the cause? I ran a quick profile, and Visual Studio seems to think that over 90% of my time was spent in D3D9_43.dll, suggesting that it's not a bug in my app, which doesn't explain why it manifests in only some builds. I rebooted my machine and it's back up to 1800FPS. I think it's a bug in the DirectX SDK tools (amongst many others). Going to delete this question.

    Read the article

< Previous Page | 3 4 5 6 7 8 9 10 11 12 13 14  | Next Page >