Thread: Local Classes

  1. #1
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Local Classes

    The Asio author told me they can't be used as template arguments pre-c++0x. This code runs fine with VC9, but fails with MinGW-GCC 3.4.5. It basically just creates a local class/struct with the goal of having a method to bind and call back later, without having to go outside the scope and declare the entire class/operator() or anonymous namespace. I assume boost::bind/function is/has to use an internal template with the local class as an argument, which is why this fails.

    Anybody have a trick up their sleeves to get this or something similar working?

    Code:
    #include <iostream>
    #include <boost/bind.hpp>
    #include <boost/function.hpp>
    
    #define CLASS(name, data) struct name { void Call() { data }; }
    
    int main()
    {
        CLASS(blah, { std::cout << "hi" << std::endl; }) temp;
    
        boost::function<void()> f = boost::bind(&blah::Call, temp);
    
        f();
    }
    In function `int main()':
    error: no matching function for call to `bind(void (main()::blah::*)(), main()::blah&)'
    === Build finished: 1 errors, 0 warnings ===
    Is that the error pre-c++0x GCC throws when it gets confused with the local class reference? ie. main()::blah. Yes, this is my crappy replacement for not having local functions, lambdas, or anything decent. Please don't mention Boost.Lambda.

    Thanks for any info
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Pre-C++0x local classes are nearly completely useless, sorry.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by CornedBee View Post
    Pre-C++0x local classes are nearly completely useless, sorry.
    Darn. Well, coroutines look like they might manage. I just want certain callbacks nested inside functions.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Local and Roaming Profiles
    By BobS0327 in forum Tech Board
    Replies: 6
    Last Post: 01-20-2008, 09:03 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM