BrainFuck Interpreter in C++ Generic Headers

#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;

typedef std::string const           BFProgram;


BF Interpreter

class BFInterpretor
{
    static const std::size_t MemorySize = 10000;

    std::vector&lt;char&gt;           memory;
    std::vector&lt;std::size_t&gt;    openLoops;
    BFProgram&amp;                  program;
    std::istream&amp;               input;
    std::ostream&amp;               output;
    std::size_t                 dataPointer;
    std::size_t                 progPointer;

    std::size_t scanToEndOfLoop()
    {
        std::size_t nestedLoops = 1;
        std::size_t scanPos     = progPointer;
        for(;nestedLoops != 0;++scanPos)
        {
            switch(program[scanPos])
            {
                case '[':   ++nestedLoops;  break;
                case ']':   --nestedLoops;  break;
                default:                    break;
            }
        }
        return scanPos;
    }

    public:
        BFInterpretor(BFProgram&amp; program, std::istream&amp; input = std::cin, std::ostream&amp; output = std::cout)
            : memory(MemorySize)
            , program(program)
            , input(input)
            , output(output)
            , dataPointer(0)
            , progPointer(0)
        {}

        static bool bfComment(char c)
        {
            switch(c)
            {
                case '&gt;': case '&lt;': case '.': case ',': case '+': case '-': case '[': case ']':
                    return false;
                default:
                    break;
             }
             return true;
        }
        void run()
        {
            while(progPointer &lt; program.size())
            {
                ++progPointer;
                switch(program[progPointer - 1])
                {
                    case '&gt;':   dataPointer = (dataPointer + 1) % MemorySize;break;
                    case '&lt;':   dataPointer = (dataPointer - 1) % MemorySize;break;
                    case '+':   ++(memory[dataPointer]);                     break;
                    case '-':   --(memory[dataPointer]);                     break;
                    case '.':   output.put(memory[dataPointer]);             break;
                    case ',':   memory[dataPointer] = input.get();           break;
                    case '[':   if (memory[dataPointer]) {
                                    openLoops.push_back(progPointer);
                                }
                                else {
                                    progPointer = scanToEndOfLoop();
                                }
                                break;
                    case ']':   if (memory[dataPointer]) {
                                    progPointer = openLoops.back();
                                }
                                else {
                                    openLoops.pop_back();
                                }
                                break;
                    default:    break; /* Ignore all comments */
                }
            }
        }
};


Program Loader

BFProgram loadProgram(std::istream&amp; program)
{
    std::string  programText;

    std::string line;
    while(std::getline(program, line))
    {
        line.erase(std::remove_if(std::begin(line), std::end(line), [](char c){return BFInterpretor::bfComment(c);}), std::end(line));
        programText.append(line);
    }
    return programText;
}
BFProgram loadProgram(std::string const&amp; file)
{
    if (file == "-") {
        return loadProgram(std::cin);
    }
    std::ifstream   program(file);
    return loadProgram(program);
}


Main

int main(int argc, char* argv[])
{
    std::vector&lt;std::string&gt;    args(argv+1 , argv+argc);
    if (args.empty()) {
        args.push_back("-");
    }

    for(std::string const&amp; file: args) {
        std::string     program = loadProgram(file);
        BFInterpretor   interpretor(program);
        interpretor.run();
    }
}
An malformed program (with misbalanced brackets) may cause an UB (e.g. popping back from an empty vector; or running out of program space in scanToEndOfLoops).
I recommend implementing step method:

void run() {
    while (progPointer &lt; program.size()) {
        step();
    }
}


with an intention to reuse it in a debugger.