Search Results

Search found 35 results on 2 pages for 'dehmann'.

Page 2/2 | < Previous Page | 1 2 

  • How to treat Base* pointer as Derived<T>* pointer?

    - by dehmann
    I would like to store pointers to a Base class in a vector, but then use them as function arguments where they act as a specific class, see here: #include <iostream> #include <vector> class Base {}; template<class T> class Derived : public Base {}; void Foo(Derived<int>* d) { std::cerr << "Processing int" << std::endl; } void Foo(Derived<double>* d) { std::cerr << "Processing double" << std::endl; } int main() { std::vector<Base*> vec; vec.push_back(new Derived<int>()); vec.push_back(new Derived<double>()); Foo(vec[0]); Foo(vec[1]); delete vec[0]; delete vec[1]; return 0; } This doesn't compile: error: call of overloaded 'Foo(Base*&)' is ambiguous Is it possible to make it work? I need to process the elements of the vector differently, according to their int, double, etc. types.

    Read the article

  • [c++] How to create a std::ofstream to a temp file?

    - by dehmann
    Okay, mkstemp is the preferred way to create a temp file in POSIX. But it opens the file and returns an int, which is a file descriptor. From that I can only create a FILE*, but not an std::ofstream, which I would prefer in C++. (Apparently, on AIX and some other systems, you can create an std::ofstream from a file descriptor, but my compiler complains when I try that.) I know I could get a temp file name with tmpnam and then open my own ofstream with it, but that's apparently unsafe due to race conditions, and results in a compiler warning (g++ v3.4. on Linux): warning: the use of `tmpnam' is dangerous, better use `mkstemp' So, is there any portable way to create an std::ofstream to a temp file?

    Read the article

  • Is call to function object inlined?

    - by dehmann
    In the following code, Foo::add calls a function via a function object: struct Plus { inline int operator()(int x, int y) const { return x + y; } }; template<class Fct> struct Foo { Fct fct; Foo(Fct f) : fct(f) {} inline int add(int x, int y) { return fct(x,y); // same efficiency adding directly? } }; Is this the same efficiency as calling x+y directly in Foo::add? In other words, does the compiler typically directly replace fct(x,y) with the actual call, inlining the code, when compiling with optimizations enabled?

    Read the article

  • Why does string::find return size_type and not an iterator?

    - by dehmann
    In C++, why does string::find return size_type and not an iterator? It would make sense because functions like string::replace or string::insert take iterators as input, so you could find some character and immediately pass the returned iterator to replace, etc. Also, std::find returns an iterator -- why is std::string::find different?

    Read the article

  • How to initialize std::vector from C-style array?

    - by dehmann
    What is the cheapest way to initialize a std::vector from a C-style array? Example: In the following class, I have a vector, but due to outside restrictions, the data will be passed in as C-style array: class Foo { std::vector<double> w_; public: void set_data(double* w, int len){ // how to cheaply initialize the std::vector? } Obviously, I can call w_.resize() and then loop over the elements, or call std::copy(). Are there any better methods?

    Read the article

  • How to find out how namespace got polluted?

    - by dehmann
    Consider the following little piece of code: // all of these include other headers, lots of code: #include "myheader1.h" #include "myheader2.h" #include <string> void foo() { string s("hello world"); // oh no, why does this compile?? } This compiles, so obviously some of the recursively included header files has a using namespace std; somewhere. How would you go about finding out where that offending line of code is? Just using grep on all header files won't really work because that statement is often used inside a function, where it is safe and won't pollute the rest of the code.

    Read the article

  • Do I really need to return Type::size_type?

    - by dehmann
    I often have classes that are mostly just wrappers around some STL container, like this: class Foo { public: typedef std::vector<whatever> Vec; typedef Vec::size_type; const Vec& GetVec() { return vec_; } size_type size() { return vec_.size() } private: Vec vec_; }; I am not so sure about returning size_type. Often, some function will call size() and pass that value on to another function and that one will use it and maybe pass it on. Now everyone has to include that Foo header, although I'm really just passing some size value around, which should just be unsigned int anyway ...? What is the right thing to do here? Is it best practice to really use size_type everywhere?

    Read the article

< Previous Page | 1 2