idioms

How to read a file into a vector elegantly and efficiently?

梦想与她 提交于 2019-12-11 07:13:17
问题 #include <fstream> #include <vector> #include <algorithm> #include <iterator> using namespace std; vector<char> f1() { ifstream fin{ "input.txt", ios::binary }; return { istreambuf_iterator<char>(fin), istreambuf_iterator<char>() }; } vector<char> f2() { vector<char> coll; ifstream fin{ "input.txt", ios::binary }; char buf[1024]; while (fin.read(buf, sizeof(buf))) { copy(begin(buf), end(buf), back_inserter(coll)); } copy(begin(buf), begin(buf) + fin.gcount(), back_inserter(coll)); return coll

Opaque reference instead of PImpl. Is it possible?

只愿长相守 提交于 2019-12-11 03:32:37
问题 The PIMPL Idiom is a technique for implementation hiding in which a public class wraps a structure or class that cannot be seen outside the library the public class is part of. This hides internal implementation details and data from the user of the library. But is it possible to implement the same making use of reference? MCanvasFont.h namespace Impl { class FontDelegate; } class MCanvasFont { public: MCanvasFont(); virtual ~MCanvasFont(); protected: // Reference count long m_cRef; // agg

A better idiom for referring to base classes from derived classes?

巧了我就是萌 提交于 2019-12-11 02:22:11
问题 Suppose I have a template <typename T> class A : class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>, anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T> { ... } Now, in class A's definition, and/or in its methods, I need to refer to the two superclasses (e.g. to access members in the superclass, or types defined in it etc.) However, I want to avoid repeating the superclass names. At the moment, what I'm doing is

Polymorphic templated classes with polymorphic template parameters

青春壹個敷衍的年華 提交于 2019-12-11 01:29:55
问题 In a simplistic design, a class B inherits a class A polymorphically. A templated class, Base<T> has a T* member that is used for further operations. A Derived<T> inherits from Base<T> polymorphically. What would be the syntax that allows this kind of object creation: Base<A>* a = new Derived<B>(); For further reference, the code I used looks like this (of course, the conversion does not succeed): class A { public: A() { cout<< " A "; } virtual void one() { cout<<" 1 "; } }; class B: public A

Java String Parameters

孤人 提交于 2019-12-11 01:04:16
问题 I'm coming from a .net background and want to know the accepted way of creating a method that returns a boolean and modifies a string that was passed in via parameter. I understand Strings are immutable in Java so the below snippet will always produce an empty string. I am constrained to return boolean only. Exceptions can't be thrown. If I need to wrap the String class in, say, a StringHolder, what package could I find this in. public static void DoStuff() { String msg = ""; if (GetMessage

Usefulness of covariant return types in C++ clone idiom?

妖精的绣舞 提交于 2019-12-10 16:40:28
问题 The usual clone idiom makes use of covariant return types: struct Base { virtual Base* clone(); }; struct Derived : public Base { Derived* clone(); }; I've read things to the effect that covariant return types were a later addition to C++, and older compilers may not support them. In this case the Derived class must declare its clone member function to return a Base* . Since, presumably, I'm only accessing Derived objects through Base pointers and/or references when using this idiom, what is

What's the idiomatic python equivalent of get() for lists?

无人久伴 提交于 2019-12-10 15:55:32
问题 Calling get(key) on a dictionary will return None by default if the key isn't present in a dictionary. What is the idiomatic equivalent for a list, such that if a list is of at least size of the passed in index the element is returned, otherwise None is returned? To rephrase, what's a more idiomatic/compact version of this function: def get(l, i): if i < len(l): return l[i] else: return None 回答1: Your implementation is Look Before You Leap-style. It's pythonic to execute the code and catch

Idiomatic way to check for non-zero

人盡茶涼 提交于 2019-12-10 15:45:16
问题 When I wish to check if a value is 0 in C, how is it idiomatically done? if (!num) if (num == 0) 回答1: While this is a matter of taste, I find it pretty much depends on intention. If the value is to be used as a boolean, ! is alright. If the value is counting something the equality makes more sense. if (!isVisible) {...} if (isVisible == 0) {...} // Intention not as clear as line above. if (numberOfItems == 0) {...} if (!numberOfItems) {...} // Intention not as clear as line above. 回答2: I

Is there a better alternative to this Ruby idiom?

巧了我就是萌 提交于 2019-12-10 09:34:32
问题 I'm finding myself writing this bit of code in my controllers a lot: params[:task][:completed_at] = Time.parse(params[:task][:completed_at]) if params[:task][:completed_at] Don't get hung up on what I'm doing here specifically, because the reasons change every time; but there are many circumstances where I need to check for a value in params and change it before handing it off to create or update_attributes . Repeating params[:task][:completed_at] three times feels very bad. Is there a better

Is there an idiom in Java for empty methods which exist to satisfy an interface?

纵然是瞬间 提交于 2019-12-09 07:38:13
问题 Let's say I have a class Foo implementing an interface such as MouseListener . The MouseListener interface consists of five methods but I only wish to override one of them ( mouseClicked() ). Is there a standard, idiomatic way of formatting the other methods? My inclination was to write the following: @Override public void mouseClicked(MouseEvent e) { // (...) <-- actual code here } @Override public void mouseEntered(MouseEvent e) { // Do nothing. Exists to satisfy MouseListener interface. }