Compiling a c source file through c++ code
I have a c++ program in visual studio 2015 , where I get as a input a c++ file,whichI do some changes to it. Afterwards, I want through a c++ code to make visual studio compile this file again.
Is there any way to do this (not through a command line)?
See also questions close to this topic

How to properly use std::enable_if in c++ on a constructor
This question combines several pieces of code and is a bit complicated, but I tried slimming it down as much as possible.
I am trying to use
std::enable_if
to conditionally invoke the correct constructor as a result of ambiguous function signatures when a lambda expression is used as input, but the parameters of said lambda expression can be implicitly convertible to one another.This is an attempt to build upon the following question: Here, but is sufficiently different and focuses on
std::enable_if
to merit another question. I am also providing the Live Example that works with the problem parts commented out.To inspect the argument (and result) types of the functor, I have the following class:
template <typename T> struct function_traits : public function_traits<decltype(&T::operator())> {}; // For generic types, directly use the result of the signature of its 'operator()' template <typename ClassType, typename ReturnType, typename... Args> struct function_traits<ReturnType(ClassType::*)(Args...) const> // we specialize for pointers to member function { enum { num_args = sizeof...(Args) }; typedef ReturnType result_type; template <size_t N> struct arg { typedef typename std::tuple_element<N, std::tuple<Args...>>::type type; // the ith argument is equivalent to the ith tuple element of a tuple // composed of those arguments. }; };
Then I try to run the below code, however, the
std::enable_if
part does not seem to work, but I know that everything within the brackets does (or should) work as demonstrated by the Live Example.template<typename data_type, typename Type1, typename Type2> class A { public: using a_type = std::tuple<Type1, Type2>; using b_type = std::tuple<std::size_t,std::size_t>; template<typename Lambda, typename = std::enable_if_t<std::is_same<typename function_traits<Lambda>::arg<0>::type, b_type>::value>> A(const Lambda& Initializer) { std::cout << "idx_type" << std::endl; } template<typename Lambda, typename = std::enable_if_t<std::is_same<typename function_traits<Lambda>::arg<0>::type, a_type>::value>> A(const Lambda& Initializer) { std::cout << "point_type" << std::endl; } }; int main() { auto f = [](std::tuple<long long, int>) > double { return 2; }; std::cout << std::is_same<typename function_traits<decltype(f)>::arg<0>::type, std::tuple<std::size_t, std::size_t>>::value << std::is_same<typename function_traits<decltype(f)>::arg<0>::type, std::tuple<long long, int>>::value; auto a = A<double, long long, int>{ [](std::tuple<long long, int>) > double { return 1; } }; auto b = A<double, long long, int>{ [](std::tuple<std::size_t, std::size_t>) > double { return 2; } }; }
So what am I missing? I am working off example #5 here. Thanks!

Calling functions when using parallel programming
I just have a fairly simple question about using MPI in a C++ program. Indeed, let's take this example:
#include <iostream> #include <sstream> #include <cblas.h> #include <cmath> using namespace std; #include <mpi.h> void multiply(double* x, double* y, int tai, double dot){ for(int i=0; i<tai;i=i+1){ dot=dot+x[i]*y[i]; } } int main(int argc, char* argv[]) { MPI_Init(&argc, &argv); ...
Then, I call my function "multiply" in my program to multiply two vectors but unfortunately it returns the initial value of "dot" (which is basically 0).
I just would like to know if there is anything special about calling a function when we are doing parallel programming.
PS: I know the whole program is working as it gives me good results when I directly multiply my 2 vectors in the "main" function

Chunking a big vector to smaller ones without extra heap allocation
I need to replace 3 heap allocations for 3 instances of std::vector, with only one contiguous heap allocation and then share it between those 3 vectors. These vector sizes are not going to be changed, so I don't need to be worried about allocation of new contiguous storage in case of pushing or inserting elements to them. My experimental result shows I get up to 2X speed up for different sizes, when I replace these 3 vectors of size n, with one vector of size of *3*n*.
However, I don't know exactly how to achieve the job of making smaller vectors out of the big one, without doing any extra heap allocation.
std::vector<double> * underlying_vec = new std::vector<double>(3*size, 1.0); // looking for a way to avoid heap allocation in constructor of these vectors std::vector<double> vec0(underlying_vec>begin() , underlying_vec>begin() + size); std::vector<double> vec1(underlying_vec>begin() + size , underlying_vec>begin() + 2*size); std::vector<double> vec2(underlying_vec>begin() + 2*size, underlying_vec>end());
I tried writing my own vector class, which its constructor accepts two iterators specifying begin and end of the portion of the underlying_vec storage belongs to this vector, but it doesn't sound a clean solution when it comes to the job of freeing the resources when the underlying vector is not accessible any more and solving the memory leakage problem.
Apparently using a customized allocator which is shared between these three vectors and allocates a contiguous memory once and assign it to the corresponding vectors seems a better solution, however since I've never written one, any hint or suggestion to help me start coding will be appreciated.