Search Results

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

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

  • How to pass data to a C++0x lambda function that will run in a different thread?

    - by Dimitri C.
    In our company we've written a library function to call a function asynchronously in a separate thread. It works using a combination of inheritance and template magic. The client code looks as follows: DemoThread thread; std::string stringToPassByValue = "The string to pass by value"; AsyncCall(thread, &DemoThread::SomeFunction, stringToPassByValue); Since the introduction of lambda functions I'd like to use it in combination with lambda functions. I'd like to write the following client code: DemoThread thread; std::string stringToPassByValue = "The string to pass by value"; AsyncCall(thread, [=]() { const std::string someCopy = stringToPassByValue; }); Now, with the Visual C++ 2010 this code doesn't work. What happens is that the stringToPassByValue is not copied. Instead the "capture by value" feature passes the data by reference. The result is that if the function is executed after stringToPassByValue has gone out of scope, the application crashes as its destructor is called already. So I wonder: is it possible to pass data to a lambda function as a copy? Note: One possible solution would be to modify our framework to pass the data in the lambda parameter declaration list, as follows: DemoThread thread; std::string stringToPassByValue = "The string to pass by value"; AsyncCall(thread, [=](const std::string stringPassedByValue) { const std::string someCopy = stringPassedByValue; } , stringToPassByValue); However, this solution is so verbose that our original function pointer solution is both shorter and easier to read. Update: The full implementation of AsyncCall is too big to post here. In short, what happens is that the AsyncCall template function instantiates a template class holding the lambda function. This class is derived from a base class that contains a virtual Execute() function, and upon an AsyncCall() call, the function call class is put on a call queue. A different thread then executes the queued calls by calling the virtual Execute() function, which is polymorphically dispatched to the template class which then executes the lambda function.

    Read the article

  • C++0x: how to get variadic template parameters without reference?

    - by Sydius
    Given the following contrived (and yes, terrible) example: template<typename... Args> void something(Args... args) { std::tuple<Args...> tuple; // not initializing for sake of example std::get<0>(tuple) = 5; } It works if you call it like so: int x = 10; something<int>(x); However, it does not work if you call it like this: int x = 10; something<int&>(x); Because of the assignment to 5. Assuming that I cannot, for whatever reason, initialize the tuple when it is defined, how might I get this to work when specifying the type as a reference? Specifically, I would like the tuple to be std::tuple<int> even when Args... is int&.

    Read the article

  • What good programming practices will change with C++0x?

    - by Jon
    For example, "Don't return objects by value if they are expensive to copy" (RVO can't always be used). This advice might change because of rvalue references. The same might be said about storing collections of pointers to objects, because copying them by value into the collection was too expensive; this reason might no longer be valid. Or the use of enums might be discouraged in favour of "enum class". What other practices or tips will change?

    Read the article

  • initializer_list in the VC10

    - by user335870
    hi i wrote this program in VC++ 2010: class class1 { public: class1 (initializer_list<int> a){}; int foo; float Bar; }; void main() { class1 c = {2,3}; getchar(); } but i get this errors when i compile project: Error 1 error C2552: 'c' : non-aggregates cannot be initialized with initializer list c:\users\pswin\documents\visual studio 2010\projects\test_c++0x\test_c++0x\main.cpp 27 and 2 IntelliSense: initialization with '{...}' is not allowed for object of type "class1" c:\users\pswin\documents\visual studio 2010\projects\test_c++0x\test_c++0x\main.cpp 27 what is the problem?

    Read the article

  • Detect template presence at compilation time

    - by doublep
    GCC up to 4.5 doesn't have standard C++0x type trait template has_nothrow_move_constructor. I could use it in my package for optimization, but I don't want to rule out one of the common compilers and don't want to overload configuration with symbols like HAVE_STD_HAS_NOTHROW_MOVE_CONSTRUCTOR. Is it somehow possible to use that template if present and just fall back to copying if not present without using any predefined configuration symbols? I also don't want to depend on Boost, since my library is small and doesn't need Boost for any other reasons. In pseudocode, I need something like: template <typename type> struct has_nothrow_move_constructor_robust : public integral_constant <bool, /* if possible */ has_nothrow_move_constructor <type>::value /* otherwise */ false> { }; Since move constructors are only for C++0x anyway, I don't mind using other C++0x features for the above definition, if at all possible.

    Read the article

  • Correct use of boost lambda

    - by Niels P.
    Consider the following piece of C++0x code: a_signal.connect([](int i) { if(boost::any_cast<std::string>(_buffer[i]) == "foo") { base_class<>* an_object = new derived_class(); an_object->a_method(_buffer[i]); }}); How would it correctly look in Boost Lambda (since this C++0x feature can't be used yet)?

    Read the article

  • Can I add Boost source+header files to my own (Open Source) project?

    - by rubenvb
    Is it allowed by the Boost License to just add the source code of the stuff I need to my project (accompanied by the license of course?). I couldn't find any "descriptive" confirmation. I would have seperate Include/boost and Source/boost directories for easy access. PS: Seeing as boost::filesystem is going into C++0x TR2, and lambda's are already in c++0x TR1, I don't see any reason to be juggling with C functions like realpath, getcwd and the like. They don't work well anyways...

    Read the article

  • Do you know of some performances test of the different ways to get thread local storage in C++?

    - by Vicente Botet Escriba
    I'm doing a library that makes extensive use of a thread local variable. Can you point to some benchmarks that test the performances of the different ways to get thread local variables in C++: C++0x thread_local variables compiler extension (Gcc __thread, ...) boost::threads_specific_ptr pthread Windows ... Does C++0x thread_local performs much better on the compilers providing it?

    Read the article

  • forward/strong enum in VS2010

    - by Noah Roberts
    At http://blogs.msdn.com/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx there is a table showing C++0x features that are implemented in 2010 RC. Among them are listed forwarding enums and strongly typed enums but they are listed as "partial". The main text of the article says that this means they are either incomplete or implemented in some non-standard way. So I've got VS2010RC and am playing around with the C++0x features. I can't figure these ones out and can't find any documentation on these two features. Not even the simplest attempts compile. enum class E { test }; int main() {} fails with: 1e:\dev_workspace\experimental\2010_feature_assessment\2010_feature_assessment\main.cpp(518): error C2332: 'enum' : missing tag name 1e:\dev_workspace\experimental\2010_feature_assessment\2010_feature_assessment\main.cpp(518): error C2236: unexpected 'class' 'E'. Did you forget a ';'? 1e:\dev_workspace\experimental\2010_feature_assessment\2010_feature_assessment\main.cpp(518): error C3381: 'E' : assembly access specifiers are only available in code compiled with a /clr option 1e:\dev_workspace\experimental\2010_feature_assessment\2010_feature_assessment\main.cpp(518): error C2143: syntax error : missing ';' before '}' 1e:\dev_workspace\experimental\2010_feature_assessment\2010_feature_assessment\main.cpp(518): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== int main() { enum E : short; } Fails with: 1e:\dev_workspace\experimental\2010_feature_assessment\2010_feature_assessment\main.cpp(513): warning C4480: nonstandard extension used: specifying underlying type for enum 'main::E' 1e:\dev_workspace\experimental\2010_feature_assessment\2010_feature_assessment\main.cpp(513): error C2059: syntax error : ';' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== So it seems it must be some totally non-standard implementation that has allowed them to justify calling this feature "partially" done. How would I rewrite that code to access the forwarding and strong type feature?

    Read the article

  • gcc std=gnu++0x option

    - by Neeraj
    Hi everyone, I need to compile a C++ code that uses std=gnu++0x option to the g++ compiler in the Makefile.am , As this option is compatible only with gcc 4.3 and above, the build crashes on my machine where i have gcc 4.2. What are my alternatives ? I tried removing that option from the Makefile.am but that reports some other error. Do i need to install gcc 4.3 or above? How can I do it in ubuntu hardy through apt-get ? Thanks.

    Read the article

  • Compilation Error on Recursive Variadic Template Function

    - by Maxpm
    I've prepared a simple variadic template test in Code::Blocks, but I'm getting an error: No matching function for call to 'OutputSizes()' Here's my source code: #include <iostream> #include <typeinfo> using namespace std; template <typename FirstDatatype, typename... DatatypeList> void OutputSizes() { std::cout << typeid(FirstDatatype).name() << ": " << sizeof(FirstDatatype) << std::endl; OutputSizes<DatatypeList...>(); } int main() { OutputSizes<char, int, long int>(); return 0; } I'm using GNU GCC with -std=C++0x. Using std=gnu++0x makes no difference.

    Read the article

  • Function return type style

    - by JB
    I'm learning c++0x, at least the parts supported by the Visual C++ Express 2010 Beta. This is a question about style rather than how it works. Perhaps it's too early for style and good practice to have evolved yet for a standard that isn't even released yet... In c++0x you can define the return type of a method using - type at the end of the function instead of putting the type at the start. I believe this change in syntax is required due to lambdas and some use cases of the new decltype keyword, but you can use it anywhere as far as I know. // Old style int add1(int a, int b) { return a + b; } // New style return type auto add2(int a, int b) -> int { return a + b; } My question really then, is given that some functions will need to be defined in the new way is it considered good style to define all functions in this way for consistency? Or should I stick to only using it when necessary?

    Read the article

  • What is the rationale to non allow overloading of C++ conversions operator with non-member functio

    - by Vicente Botet Escriba
    C++0x has added explicit conversion operators, but they must always be defined as members of the Source class. The same applies to the assignment operator, it must be defined on the Target class. When the Source and Target classes of the needed conversion are independent of each other, neither the Source can define a conversion operator, neither the Target can define a constructor from a Source. Usually we get it by defining a specific function such as Target ConvertToTarget(Source& v); If C++0x allowed to overload conversion operator by non member functions we could for example define the conversion implicitly or explicitly between unrelated types. template < typename To, typename From operator To(const From& val); For example we could specialize the conversion from chrono::time_point to posix_time::ptime as follows template < class Clock, class Duration operator boost::posix_time::ptime( const boost::chrono::time_point& from) { using namespace boost; typedef chrono::time_point time_point_t; typedef chrono::nanoseconds duration_t; typedef duration_t::rep rep_t; rep_t d = chrono::duration_cast( from.time_since_epoch()).count(); rep_t sec = d/1000000000; rep_t nsec = d%1000000000; return posix_time::from_time_t(0)+ posix_time::seconds(static_cast(sec))+ posix_time::nanoseconds(nsec); } And use the conversion as any other conversion. So the question is: What is the rationale to non allow overloading of C++ conversions operator with non-member functions?

    Read the article

  • If the address of a function can not be resolved during deduction, is it SFINAE or a compiler error?

    - by Faisal Vali
    In C++0x SFINAE rules have been simplified such that any invalid expression or type that occurs in the "immediate context" of deduction does not result in a compiler error but rather in deduction failure (SFINAE). My question is this: If I take the address of an overloaded function and it can not be resolved, is that failure in the immediate-context of deduction? (i.e is it a hard error or SFINAE if it can not be resolved)? Here is some sample code: struct X { // template T* foo(T,T); // lets not over-complicate things for now void foo(char); void foo(int); }; template struct S { template struct size_map { typedef int type; }; // here is where we take the address of a possibly overloaded function template void f(T, typename size_map::type* = 0); void f(...); }; int main() { S s; // should this cause a compiler error because 'auto T = &X::foo' is invalid? s.f(3); } Gcc 4.5 states that this is a compiler error, and clang spits out an assertion violation. Here are some more related questions of interest: Does the FCD-C++0x clearly specify what should happen here? Are the compilers wrong in rejecting this code? Does the "immediate-context" of deduction need to be defined a little better? Thanks!

    Read the article

  • What is the rationale to not allow overloading of C++ conversions operator with non-member function

    - by Vicente Botet Escriba
    C++0x has added explicit conversion operators, but they must always be defined as members of the Source class. The same applies to the assignment operator, it must be defined on the Target class. When the Source and Target classes of the needed conversion are independent of each other, neither the Source can define a conversion operator, neither the Target can define a constructor from a Source. Usually we get it by defining a specific function such as Target ConvertToTarget(Source& v); If C++0x allowed to overload conversion operator by non member functions we could for example define the conversion implicitly or explicitly between unrelated types. template < typename To, typename From > operator To(const From& val); For example we could specialize the conversion from chrono::time_point to posix_time::ptime as follows template < class Clock, class Duration> operator boost::posix_time::ptime( const boost::chrono::time_point<Clock, Duration>& from) { using namespace boost; typedef chrono::time_point<Clock, Duration> time_point_t; typedef chrono::nanoseconds duration_t; typedef duration_t::rep rep_t; rep_t d = chrono::duration_cast<duration_t>( from.time_since_epoch()).count(); rep_t sec = d/1000000000; rep_t nsec = d%1000000000; return posix_time::from_time_t(0)+ posix_time::seconds(static_cast<long>(sec))+ posix_time::nanoseconds(nsec); } And use the conversion as any other conversion. For a more complete description of the problem, see here or on my Boost.Conversion library.. So the question is: What is the rationale to non allow overloading of C++ conversions operator with non-member functions?

    Read the article

  • `enable_shared_from_this` has a non-virtual destructor

    - by Shtééf
    I have a pet project with which I experiment with new features of the upcoming C++0x standard. While I have experience with C, I'm fairly new to C++. To train myself into best practices, (besides reading a lot), I have enabled some strict compiler parameters (using GCC 4.4.1): -std=c++0x -Werror -Wall -Winline -Weffc++ -pedantic-errors This has worked fine for me. Until now, I have been able to resolve all obstacles. However, I have a need for enable_shared_from_this, and this is causing me problems. I get the following warning (error, in my case) when compiling my code (probably triggered by -Weffc++): base class ‘class std::enable_shared_from_this<Package>’ has a non-virtual destructor So basically, I'm a bit bugged by this implementation of enable_shared_from_this, because: A destructor of a class that is intended for subclassing should always be virtual, IMHO. The destructor is empty, why have it at all? I can't imagine anyone would want to delete their instance by reference to enable_shared_from_this. But I'm looking for ways to deal with this, so my question is really, is there a proper way to deal with this? And: am I correct in thinking that this destructor is bogus, or is there a real purpose to it?

    Read the article

  • Tips for submitting a library to Boost?

    - by AraK
    Hi everyone, Summer is coming, and a group of friends and I are getting ready for it :) We decided to build a compile-time Arbitrary precision Unsigned Integers. We would like to provide a set of integers algorithms(functions) with the library. We have seen a number of requests for such a library(SoC2010, C++0x Standard Library wishlist). Also, a regular run-time bigint is requested usually with that, but we don't want to go into the hassle of memory management. The idea came to me from a library called TTMath, unfortunately this library works only on specific platforms because Assembly was used extensively in the library. We would like to write a standard library, depending on the C++ standard library and Boost. Also, we would like to use the available C++0x facilities in current compilers like user-defined literals and others. This would technically make the library non-standard for a while, but we think that it is a matter of time the new standards will be official. Your hints on the whole process including design, implementation, documentation, maintainable of the library are more than welcom. We are a group of students and fresh graduates who are looking for something interesting in the summer, but we see that Boost is full of gurus and we don't want to forget something too obvious. We are communicating on-line, so there is no shared white-boards :( Thanks,

    Read the article

  • Making swap faster, easier to use and exception-safe

    - by FredOverflow
    I could not sleep last night and started thinking about std::swap. Here is the familiar C++98 version: template <typename T> void swap(T& a, T& b) { T c(a); a = b; b = c; } If a user-defined class Foo uses external ressources, this is inefficient. The common idiom is to provide a method void Foo::swap(Foo& other) and a specialization of std::swap<Foo>. Note that this does not work with class templates since you cannot partially specialize a function template, and overloading names in the std namespace is illegal. The solution is to write a template function in one's own namespace and rely on argument dependent lookup to find it. This depends critically on the client to follow the "using std::swap idiom" instead of calling std::swap directly. Very brittle. In C++0x, if Foo has a user-defined move constructor and a move assignment operator, providing a custom swap method and a std::swap<Foo> specialization has little to no performance benefit, because the C++0x version of std::swap uses efficient moves instead of copies: #include <utility> template <typename T> void swap(T& a, T& b) { T c(std::move(a)); a = std::move(b); b = std::move(c); } Not having to fiddle with swap anymore already takes a lot of burden away from the programmer. Current compilers do not generate move constructors and move assignment operators automatically yet, but as far as I know, this will change. The only problem left then is exception-safety, because in general, move operations are allowed to throw, and this opens up a whole can of worms. The question "What exactly is the state of a moved-from object?" complicates things further. Then I was thinking, what exactly are the semantics of std::swap in C++0x if everything goes fine? What is the state of the objects before and after the swap? Typically, swapping via move operations does not touch external resources, only the "flat" object representations themselves. So why not simply write a swap template that does exactly that: swap the object representations? #include <cstring> template <typename T> void swap(T& a, T& b) { unsigned char c[sizeof(T)]; memcpy( c, &a, sizeof(T)); memcpy(&a, &b, sizeof(T)); memcpy(&b, c, sizeof(T)); } This is as efficient as it gets: it simply blasts through raw memory. It does not require any intervention from the user: no special swap methods or move operations have to be defined. This means that it even works in C++98 (which does not have rvalue references, mind you). But even more importantly, we can now forget about the exception-safety issues, because memcpy never throws. I can see two potential problems with this approach: First, not all objects are meant to be swapped. If a class designer hides the copy constructor or the copy assignment operator, trying to swap objects of the class should fail at compile-time. We can simply introduce some dead code that checks whether copying and assignment are legal on the type: template <typename T> void swap(T& a, T& b) { if (false) // dead code, never executed { T c(a); // copy-constructible? a = b; // assignable? } unsigned char c[sizeof(T)]; std::memcpy( c, &a, sizeof(T)); std::memcpy(&a, &b, sizeof(T)); std::memcpy(&b, c, sizeof(T)); } Any decent compiler can trivially get rid of the dead code. (There are probably better ways to check the "swap conformance", but that is not the point. What matters is that it's possible). Second, some types might perform "unusual" actions in the copy constructor and copy assignment operator. For example, they might notify observers of their change. I deem this a minor issue, because such kinds of objects probably should not have provided copy operations in the first place. Please let me know what you think of this approach to swapping. Would it work in practice? Would you use it? Can you identify library types where this would break? Do you see additional problems? Discuss!

    Read the article

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