vendredi 28 octobre 2016

std::packaged_task from a lambda function with a parameter by reference

I want to package a simple function that reads a file and does some other things to it to pass it to a thread. The following gives me an error in g++.

std::packaged_task<int(std::string,std::string)> readFileTask(
                              [](const std::string& filename,
                                  std::string& filedata) -> int
        {
            //call something like ReadFile(filename,filedata);
            //the second parameter will get the data from the file
            //do other stuff
        });

The error is not so informative:

/usr/include/c++/4.9/functional:1665: error: no type named 'type' in 'class std::result_of<std::reference_wrapper<MainWindow::sendFile(const QString&)::<lambda(const string&, std::string&)> >(std::basic_string<char>, std::basic_string<char>)>'
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^

This can be fixed by removing the non-const string. The following compiles!

std::packaged_task<int(std::string)> readFileTask(
                              [](const std::string& filename) -> int
        {
        });

So it's just that when there's a non-const reference, it complains. Why?

How can I package a lambda with a non-const reference?

C++ 11 usage unclear

In the following C++ code what does " double (*) double " mean ? What kind of a return type it is ?

auto get_fun(int arg) -> double (*)(double) // same as: double (*get_fun(int))(double)
{
    switch (arg)
    {
        case 1: return std::fabs;
        case 2: return std::sin;
        default: return std::cos;
    }
}

people to ask what to do to be able to read more than 30 characters

people to ask what to do to be able to read more than 30 characters turn from binary file in C ++, I am currently using this syntax: data.write(reinterpret_cast(&liststudent[i]), sizeof(student)); and now it is an error, expect people to help thanks you !

return different data type without explicitly specify data type in template

I want to do something like this:

template<class T>
T foo(uint8_t x)
{
    if (x<32) return (int32_t)(1<<x);
    else return (int64_t)(1<<x);
}

but I do not want to call foo<int32_t>(x) or foo<int64_t>(x). It just doesn't look nice.I want the return type to be automatically deduced correctly.

If macros can achieve the same, I'm happy to use macros. Any suggestions?

make_tuple with template parameters does not compile

Consider this code:

#include <tuple>

int main()
{
    int i;
    long k;

    auto tup1 = std::make_tuple<long>(i);   // Compiles
    auto tup2 = std::make_tuple<int>(k);    // Compiles
    auto tup3 = std::make_tuple<int>(i);    // Does not compile
    auto tup4 = std::make_tuple<int>(i+0);  // Compiles
    auto tup5 = std::make_tuple(i);         // Compiles
}

Why does auto tup3 = ... not compile? Apparently, make_tuple<int>(...) wants an rvalue reference as its argument; but why?

(I'm using GCC 6.1.0.)

'unordered_map' file not found error when compiling with Xcode 7.3.1

A file in my project fails to compile with

'unordered_map' file not foundIn file included from ...

This is despite I compile with -std=c++ and a directory that has unordered_map in it is in system include paths.

clang -std=c++14 -x c++ -v -E /dev/null

results in the output below. /Applications/Xcode_7.http://ift.tt/2ehLkIn does contain unordered_map.

Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode_7.http://ift.tt/2dO8gTo
"/Applications/Xcode_7.http://ift.tt/2ehK1JG" -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -main-file-name null -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 264.3.102 -v -dwarf-column-info -resource-dir /Applications/Xcode_7.http://ift.tt/2dO5zkZ -stdlib=libc++ -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /Users/ovz -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o - -x c++ /dev/null
clang -cc1 version 7.3.0 (clang-703.0.31) default target x86_64-apple-darwin15.6.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here: 
#include <...> search starts here: /Applications/Xcode_7.http://ift.tt/2ehLkIn
/usr/local/include
/Applications/Xcode_7.http://ift.tt/2ehMPGA
/Applications/Xcode_7.http://ift.tt/2dO8MRt
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.

Using getter, setter in c++

i wanna use class in c++ what has a integer array like it:

class A{
private:
        int arr[50];

}

and i will read something from text file like it:

sum i1 i2

thats means:sum of arrays index1 and index2 and store in index1

so, how can i do it, with using getters and setters like:

seti2(geti1()+geti2())

or something like it, ( because its not verys useful, i dont want to write getter and setter for every index geti1() geti2() ... geti50() )

do you have any idea?

by the way, my second question is that, is getter should not have any parameters and is setter should have only one parameter?