I have a couple of questions about various things that have had me puzzled. Not that I really need to know, but I'm just curious, thats all...

1. It is possible to write a macro called JOIN that joins macro arguments together such as
Code:
#include <iostream>
#define JOIN(a,b) a ## b
int main() {
    JOIN(std::co,ut <<) "hello"; // will print "hello"
}
Is it possible to modify these input arguments in the macro. Ie is a macro REVERSE possible eg
Code:
#include <iostream>
#define REVERSE(a) ???? // is this possible?
int main() {
    REVERSE(<< tuoc::dts) "hello"; // prints hello
}
2. I have written a function to calculate the average of a vector of numbers. It is templated so that it works on any type of value. However is it possible to check if the template type has an addition and division operator and if not, print a compile error message?
My code:
Code:
template <typename T>
T average<T>(std::vector<T> numbers)
{
    // #if T does not have + and / , #error "cannot compute average"
    double sum = std::accumulate(numbers.begin(), numbers.end(), 0);
    return sum / numbers.size();
}
3. In some functions in the boost library, I have noticed that they appear to return a type (eg boost::result_of). How is this possible?
Example:
Code:
#include <boost/utility/result_of.hpp>
#include <iostream>
template<typename Function>
std::string typeTest(Function f) {
    boost::result_of<Function()>::type output;  // how does this work?
    return typeid(output).name();
}


int main()
{
    std::cout << typeTest(&main); // prints "int"
    std::cin.get();
}
4. How does typeid work internally? Is the answer computed at runtime or compile time? Also, does using it result in extra memory being used to store type information in classes?

5. Is it possible to have a function return a stack object without the created stack object being copied and deleted?
Eg
Code:
#include <iostream>
class TestClass {
public:
    int value;

    TestClass() {
        value = 0;
        std::cout << "Constructor\n";
    }

    TestClass(TestClass& rhs) {
        value = rhs.value;
        std::cout << "Copy Constructor\n";
    }

    void setValue(int num) {
        value = num;
    }

    ~TestClass() {
        std::cout << "deconstructor\n";
    }
};

TestClass test() {
    TestClass tc;
    tc.setValue(3);
    return tc;
}

void test2() {
    TestClass t = test();
}

int main() {
    test2();
    std::cin.get();
	return 0;
}
Output:
Code:
Constructor
Copy Constructor
deconstructor
deconstructor
There doesn't seem to be any reason for the compiler to copy the object as it is only being used in the caller function.