Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Lv 614,418 points

David

Favorite Answers42%
Answers2,078

A JavaScript and C++ enthusiast. If you have any questions or problems, email me: david.godfrey54@yahoo.com

  • What does it mean to use *67 in a phone call?

    What will the person see in the caller ID??

    1 AnswerCell Phones & Plans8 years ago
  • C++: How do I make a function run only if the type is a tuple of vectors?

    I have the code here online and it works fine: http://paste.ubuntu.com/5793610/

    The code is supposed to create a turn a std::tuple of types into a tuple of vectors of those types. Then it's supposed to fill the vectors with numbers and then print the tuple. The only problem I have with it is that theoretically I can pass in a tuple of any types to the printer method but I just want to restrict the implementation of tuple_printer::print to tuples of *vectors*. How would I do that?

    I've tried this but it didn't work:

        template <typename... Ts>

        static void print(std::tuple<std::vector<Ts>...>& var, /* ... */)

        {

            // ...

        }

    I know why it doesn't work but do you see my intent? How would I be able to do that? Would I need some sort of variadic enable_if implementation that unpacks the parameter pack and checks if each type is a vector? That I would have a hard time doing though. :)

    Thanks.

    1 AnswerProgramming & Design8 years ago
  • C++: Why am I getting a static-assertion failure for this simple program?

    I'm trying to experiment with std::tuple_cat but for some reason in my program the types do not match. Any idea why?

    #include <tuple>

    #include <type_traits>

    int main()

    {

        int x{0}, y{0};

        auto var = std::tuple_cat(std::tie(x), std::tie(y));

        static_assert(

            std::is_same<decltype(var), std::tuple<int, int>>::value, ""

        );

    }

    1 AnswerProgramming & Design8 years ago
  • C++: Am I using this mutex correctly?

    I feel that it can be done in a better way than this.

        #include <functional>

        #include <thread>

        #include <mutex>

        #include <chrono>

        #include <iostream>

        std::mutex m;

        void f(int& n)

        {

            m.lock();

            ++n;

            m.unlock();

            std::this_thread::sleep_for( std::chrono::seconds(1) );

        }

        int main()

        {

            int n(0);

            std::function<void()> h(std::bind(f, std::ref(n)));

            std::thread f1(h);

            f1.join();

            std::this_thread::sleep_for( std::chrono::seconds(2) );

            std::cout << n; // 1

        }

    Is this a correct way of using mutexes? I am new to this by the way.

    1 AnswerProgramming & Design8 years ago
  • C++: Is std::move always invoked implicitly?

    I understand that std::move can be implicitly invoked when returning a literal that can be implicitly converted to the return type of the function surrounding it. My question is are there any cases where I will have to manually use std::move. Can using rvalue references nullify this necessity?

    1 AnswerProgramming & Design8 years ago
  • C++: How do I overload the "new" operator for placement new?

    Consider the scenario:

        T* t = new T();

        X* x = new (t) X();

    How would I do an overload of class T to allow this?

    1 AnswerProgramming & Design8 years ago
  • C++: Is there a way to call an operator function through a static method in this context?

    This is the code I have. It compiles; but I want to call the function h with the arguments of H<T>::operator(). How would I do that?

        #include <utility>

        #include <iostream>

        template <template <class> class H, typename F>

        struct N {

            N() = delete;

            static F f() {

                H<F> h;

                return h();

            }

        };

        template <class T>

        struct H {

            H() = default;

            T operator()() {

                return T();

            }

        };

        template <template <class> class T>

        int h() {

            return N<T, decltype( std::declval<T<int>>().operator()() )>::f();

        }

        int main() {

            std::cout << h<H>();

        }

    For example, instead of doing h<H>() I do h<H>(5) and it outputs 5 for me through H<int>::operator(). And that also means calling the static member function f with those arguments. Can someone propose an example in which this is possible? Thanks.

    1 AnswerProgramming & Design8 years ago
  • C++: Is there a way with templates to overload << to do a no-op on a certain class specialization?

    My class N takes a type T and a varying amount of types F...

    What's really happening is that I am overloading operator() to take a function reference and the arguments that that function will be supplied. It's like a bind function. But when the function reference returns void, I don't want to be able to std::cout the value of that function call from operator(). So I added an overload of operator<< for std::cout to NOT do anything but I guess I'm not writing out the function signature correctly because I get the same errors. Does anyone know what I'm doing wrong?

        #include <iostream>

        #include <utility>

        template <typename T>

        struct N;

        template <typename T, typename ... F>

        struct N<T(F...)> {

            T operator()(T (&t)(F...), F&&... f) {

                return t(std::forward<F>(f)...);

            }

        };

        template <typename ... T> void operator<< (std::ostream &, const N<void(T...)> &) {}

        void f(int, int) {}

        int main() {

            N<void(int, int)> bind;

            std::cout << bind(f, 5, 4);

        }

    Here's a demo -- http://ideone.com/t5eK7d#view_edit_box

    The above code fails because I'm printing out a function that returns void; and that is the function f. My overload of operator<< doesn't seem to be affecting anything. If you need any more detail just say so. Thanks.

    1 AnswerProgramming & Design8 years ago
  • How do I call a non-const member function from a const object?

    My question is pretty simple and straight-forward. I have a class S with two member functions, f and h. In f I make a const S object. However, I can't call its h method as the object is const and h is not (and the compiler can't guarantee that I won't modify the S object). I don't want to make another h function (as I'll duplicate the function) and I don't want to make the h I have const. So how would I call it? Here is the code I have:

        struct S {

            void h() {}

            void f() {

                const S & s = *this;

                s.h();

            }

        };

        int main() {

            S s;

            s.f(); // error line 8

        }

    Any and all answers are appreciated.

    2 AnswersProgramming & Design8 years ago
  • C++: Why am I getting the error "expected type-specifier" when using declval as a template argument?

    Here is the function I'm dealing with:

        template <typename T, typename C = std::declval<T>> T & f(C c = C()) {

            return c;

        }

    It's supposed to run std::declval[1] through the function f. I've pinpointed the problem down to the the default template argument. It seems the compiler is not interpreting std::declval as a type. Why? I'm getting the following errors:

        prog.cpp:17:36: error: expected type-specifier

        prog.cpp:17:36: error: expected '>'

        prog.cpp: In function 'int main()':

        prog.cpp:23:10: error: no matching function for call to 'f()'

        prog.cpp:23:19: error: no matching function for call to 'f()'

    So I tried using decltype on it:

        template <typename T, typename C = decltype( std::declval<T> )> T & f(C c = C()) {

            return c;

        }

    The errors go away until I have to call f:

        f<A>();

    (Where A is an arbitrary class; remember that I'm trying to run std::declval through the function; so try and think of the function f as std::declval<A>).

    But I get a large amount of template errors, most of them saying that C is A && when I call f with f<A>(). I never wanted C to be an rvalue reference, so then I try to take out the reference using std::remove_reference[2]:

        template <typename T, typename C = std::remove_reference<decltype( std::declval<T> )>::type> T & f(C c = C()) {

            return c;

        }

    But then I get the exact same errors. What could I be doing wrong? Why isn't std::declval<T> deduced as a type? If you guys need context, here's what I have:

        #include <utility>

        #include <type_traits>

        struct B;

        struct A {

            void operator *(const B &) {}

        };

        struct B {

            void operator *(int x) const {}

        };

        inline void operator *(int a, const B & b) {

            return b * a;

        }

        template <typename T, typename C = std::remove_reference<decltype( std::declval<T> )>::type> T & f(C c = C()) {

            return c;

        }

        int main() {

            f<A>() * f<B>();

        }

    The above code surrounding the function does nothing special. What you should be focusing on is why std::declval<T> is not a type and how I can fix this. Here is a demo:

        http://ideone.com/Zluu3d#view_edit_box

    All answers and suggestions are appreciated.

        [1]: Found in header <utility>. Converts any type T into a reference type, thus allowing the ability to use member functions without specifying constructors

        [2]: Found in header <type_traits>. Removes a given reference

  • C++: Why don't I get compiler errors from returning from a void function template?

    Consider:

        void f() {

            return 5;

        }

    The above will raise errors. But why not this?:

        template <typename = void> void f() {

            return 0;

        }

    I'm compiling with gcc-4.5.1. Why does it make a difference using templates such that I wouldn't receive errors from doing the same illegal return statement as a non-template function?. The only setback I get is that I can't call the function (i.e f()) without getting:

        error: return-statement with a value, in function returning 'void'

    But still, what could be the reason that I am able to define a return statement for a void function template?

    Here is the code I have:

        template <typename = void> void f() {

            return 0;

        }

        // pass

        int main() {

        }

    The above code will pass despite a presumably illegal return statement in a function returning void. Why?

    3 AnswersProgramming & Design9 years ago
  • Confusing syntax with anonymous template types?

        template <class T, class U> decltype(*(T*)(0) * *(U*)(0)) mul(T x, U y) {

            return x * y;

        }

    This piece of code was taken from Stroustrup's C++11 FAQ [1]. I understand what it does, which is multiply two objects of varying types. What perplexes me is the syntax between the template parameters and the function definition. What is happening inside decltype? I take it that it's dereferencing an unnammed T pointer initialized to 0, and multiplying it by an unnamed U pointer being dereferenced and initialized in the same way. Am I right?

    Well, if this is what is happening, then isn't the use of pointers, dereferences, and extra parenthesis superfluous? Couldn't I initialize the types like this while maintaining the desired effect?:

        template <class T, class U> decltype(T(0) * U(0)) mul(T x, U y) {

            return x * y;

        }

    This looks much cleaner to me, and has the same effect with multiplying as the first...

        mul(4, 3); // 12

    So why does Stroustrup insist on using the complexity of pointers, dereferencing them and initializating them like he does? This is, of course, before he introduced the new auto syntax [2]. But anyway, my question is: Is there any difference between the two above forms of type initialization? Where he uses pointers and instantly dereferences them instead of simply doing what I did, which was to initialize the types with no pointers or dereferencing? Any response is appreciated.

        [1]: http://www.stroustrup.com/C++11FAQ.html#suffix-ret...

        [2]: http://en.wikipedia.org/wiki/C%2B%2B11#Alternative...

    1 AnswerProgramming & Design9 years ago
  • C++: What are the differences between these variable declarations?

    What are the differences between the following character/array initializations? Explain how they differ with respects to how I can use them. Also, give me an example where one is guaranteed to work where the other will not.

        char a[4] = "Bus";

        const char * a = "Bus";

        "Bus"

    1 AnswerProgramming & Design9 years ago
  • C++: Function pointer ambiguity?

    Take the following example. I create a function pointer named "s", set it to f and call it. This compiles fine:

    void f() {}

    int main() {

        void (*s)();

        s = f;

        s();

    }

    But take this next example, where I declare "s" now as a reference and set to f inline. This compiles fine as well:

    void f() {}

    int main() {

        void (&s)() = f;

        s();

    }

    What is the difference between these two? Specifically the pointer and the reference syntax? I commonly use the former, but the latter seems superfluous? Having to assign s to f inline is the only way it could work in the second one, whereas the with the function "pointer" syntax I had the ability to do it both ways. In terms of usability, what can I do with the second form that I cannot do with the first?

    1 AnswerProgramming & Design9 years ago
  • What is friend in C++?

    What does the keyword friend do in C++? Please provide an example along with its description so I may understand.

    1 AnswerProgramming & Design9 years ago
  • What does this C++ code do?

    Can you please explain what this code is supposed to do? Thanks.

    template <typename T> void f( T & t, void ( T::*x )() ) {

        (t.*x)();

    }

    struct A {

        void f() {}

    };

    int main() {

        A a;

        f( a, &A::f );

    }

    1 AnswerProgramming & Design9 years ago
  • I need help with C++. Does anyone know anything about C++0x?

    I found this C++ code and I don't know what it means. Does anyone know?

    int i(5);

    int b( <:=:> () mutable throw -> int <% return i++; %> );

    Looks to me like it's a lambda. But I've never seen one that looks like it. Does anyone know what it is?

    1 AnswerProgramming & Design9 years ago