Question

For some reason when I try to std::cerr a variable in an std::tuple, my program crashes. My guess is that std::get<int>( std::tuple ) is returning garbledy-gook. Is there any reason why the values passed into an std::tuple would change, at all (upon passing in, or upon a call to std::get< int >( std::tuple ), because of some strange wording in the standard, or a faulty implementation)? e.g from a copy, something silly happens when you try to read one (i.e std::get), etc.?

EDIT: This question is meant to be abstract, and is not necessarily about the code below, but to provide information on std::tuple, I (and others who read this post) may be able to use in the future.

UPDATE: Works from MinGW GCC 4.4.1 as well.

UPDATE:
I just noticed my code runs on ideone.com:

#include <iostream>
#include <string>
#include <vector>
#include <tuple>
#include <typeinfo>
#include <stdio.h>
#include <string.h>

template< int IETORATOR_T, typename TUPLE, typename FUNCTION_POINTER_T, typename... ARGUMENTS_T >
struct FunctionRunner
{
    FunctionRunner( TUPLE* tuple, FUNCTION_POINTER_T functionToRun, ARGUMENTS_T... arguments )
    {
        std::cerr << "Hi there!\n";
        //TEST CODE.//
        ///////////////////////////////////////////
        /////I can read from the value.//
        auto j = std::forward< decltype( std::get< IETORATOR_T >( *tuple ) ) >( std::get< IETORATOR_T >( *tuple ) );
        //I can write to the value.//
        j += 2;
        std::cerr << "------------\n";
        //I cant cerr the value? 0.0//
        std::cerr << "Passing " << j << "\n";
        FunctionRunner< IETORATOR_T - 1, TUPLE, FUNCTION_POINTER_T, decltype( std::get< IETORATOR_T >( *tuple ) ), ARGUMENTS_T... > runner{ 
                tuple, functionToRun, std::get< IETORATOR_T >( *tuple ), arguments... };
    }
};
template< typename TUPLE, typename FUNCTION_POINTER_T, typename... ARGUMENTS_T >
struct FunctionRunner< ( -1 ), TUPLE, FUNCTION_POINTER_T, ARGUMENTS_T... >
{
    FunctionRunner( TUPLE* tuple, FUNCTION_POINTER_T functionToRun, ARGUMENTS_T... arguments ) {
        functionToRun( arguments... );
    }
};

template< typename... ARGUMENT_TYPES_T >
struct ArgumentMaker
{
    std::tuple< ARGUMENT_TYPES_T... >* argumentData;
    ArgumentMaker( ARGUMENT_TYPES_T... arguments ) {
        argumentData = new std::tuple< ARGUMENT_TYPES_T... >( std::forward< ARGUMENT_TYPES_T >( arguments )... );
    }
    ~ArgumentMaker() {
        delete argumentData;
    }
    template< typename FUNCTION_POINTER_T >
    void ExecuteFunction( FUNCTION_POINTER_T functionToRun ) {
        FunctionRunner< ( std::tuple_size< std::tuple< ARGUMENT_TYPES_T... > >::value - 1 ), 
                std::tuple< ARGUMENT_TYPES_T... >, FUNCTION_POINTER_T > runner{ argumentData, functionToRun };
    }
};

void Test( int a, double d, float c ) {
    std::cerr << a << " " << d << " " << c << "\n";
}

int main()
{
    int a = 2;
    double b = 34.5;
    float c = 45.6f;
    auto* maker = new ArgumentMaker< int, double, float >( a, b, c );
    maker->ExecuteFunction( &Test );
    delete maker;
    return ( 0 );
}

The compiler I am using on my desktop is MinGW's GCC 4.8.1-4. Is this a compiler bug?

Was it helpful?

Solution

Is there any reason why the values passed into an std::tuple would change, at all?

No, of course not, unless you change them. A std::tuple is just a structure that holds values as members, it doesn't have any magical properties that cause values to update silently.

You should simplify your code to something really basic instead of all that irrelevant clutter and see if that works:

#include <tuple>
#include <iostream>

int main()
{
    std::tuple<int> t{ 1 };
    std::cerr << std::get<0>(t);
}

If that doesn't work, make it even simpler:

#include <iostream>

int main()
{
    int t = 1 ;
    std::cerr << t;
}

If that doesn't work something is very broken in your MinGW installation, but it's nothing to do with tuple or variadic templates or dynamic allocation or anything else in your original code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top