jeudi 28 décembre 2017

std::ofstream crash on construction when called from GC thread

I attempt to do debug logging to a file in one of the destructors. In our application, this happens to be on the GC [Finalizer] thread (since I want to do logging in our native library that is running in a managed context - a .NET application).

The problem is, that I can't construct std::ofstream - it crashes during construction. Seems like std::locale::facet object is already destroyed.

I attach a screenshot with a relevant call stack, code and an exception:

enter image description here

The bit of code that tries to create an ofstream object is just that:

SpatialRenderer::~SpatialRenderer()
{
    // dump
    std::ofstream f("dump.txt"); // <- crash at this line
    // writing to f here
}

Environment: Windows 10, Visual Studio 2017 Version 15.4.5

Why doesn't ~x toggle bits (where x is an unsigned integer)

I want to toggle bits of an unsigned integer. Let us say I have an integer x. Why doesn't ~x return the number with toggled bits?

Calling a C++ function from Qml asynchronously or in a new thread

I completed my entire project on building an application that basically fetches sql query result based on some user input combination in the GUI and I believe that there is some performance issue when calling a Q_INVOKABLE function in C++ code which performs some computation to produce the query and also run the query and return the result, so I wanted to know if there is a way to run that function in a separate thread from qml. The skeleton is somewhat like this:

Result.h

class Result{
public:
   Q_INVOKABLE void on_submit_button_clicked(); 
}

Result.cpp

Result::on_submit_button_clicked()
{
        /*Code that prepare the query*/

        /*Code that runs the query using a Qthread*/
}

main.qml

ApplicationWindow
{
    id:appwindow


    /*Other parts of the GUI*/
    Button{
        id:submitButton
        onClicked:{
              result_object.on_submit_button_clicked() //how to run this function in a new thread ? 
              /*code to load my table view columns*/
        }
    }
}

is std::function heavier than auto storing lambda function

I heard that cost of std::function is heavier than auto to deal with a lambda function. effective modern c++ item5. What I want is to clarify the mechanism why std::function use more memory than auto with some sample code. Could somebody help me?

Why the function don't work on the string?

#include <iostream>
#include <string>
using namespace std;
void chuli(string a)
{
    a.erase(0,2);
}

int main()
{
    string a = "012345";
    a = chuli(a);
    cout << a; 
}

beginner for C++,i want to know why after this function,this string don't change.Is this something about the namespace?

Do postfix operators return lvalue?

Isn't the foll code given in C++ Primer incorrect ?

ostream_iterator<int> out_iter(cout, " ");
for (auto e : vec)
    *out_iter++ = e;  // the assignment writes this element to cout
cout << endl;

The postfix operator returns the old value, not a reference then how can it be made to act as an lvalue ?

Please correct if I am wrong

mercredi 27 décembre 2017

template substitution failure with sfinae

The following code does not compiler with GCC 5.2 or later, have not tried older compiler. Amy idea on what I am doing wrong here.

#include <iostream>
#include <string>
#include <type_traits>

template <typename T>
typename std::enable_if<std::is_enum<T>::value, T>::type
operator^=(T &lhs, const T &rhs)
{
    lhs = static_cast<T>(
                 static_cast<typename std::underlying_type<T>::type>(lhs) ^
                 static_cast<typename std::underlying_type<T>::type>(rhs));
    return lhs;
}

template <typename T, typename S>
typename std::enable_if<std::is_enum<T>::value && std::is_integral<S>::value, T>::type
operator^=(T &lhs, const S &rhs)
{
    lhs = static_cast<T>(
                 static_cast<typename std::underlying_type<T>::type>(lhs) ^
                 static_cast<typename std::underlying_type<T>::type>(rhs));
    return lhs;
}

enum Enum_t{
    Red,
    Blue,
    Green,
    Black
} ;
#define somedef 2
int main(){
    Enum_t b = Red;
    b ^= somedef;

    std::cout << std::to_string(b) << std::endl;
}

I get the following error:

g++ -std=gnu++11 main.cpp

main.cpp: In function ‘int main()’:
main.cpp:34:4: error: invalid conversion from ‘int’ to ‘Enum_t’ [-fpermissive]
b ^= somedef;