storing mem_fun in a standard container

Is there a way to create a vector< mem_fun_t< ReturnType, MyClass > > ?

The error i'm seeing is:

error C2512: 'std::mem_fun1_t<_Result,_Ty,_Arg>' : no appropriate default constructor available

-------------Problems Reply------------

You certainly can create such a vector.

#include <vector>
#include <functional>
#include <iostream>

struct MyClass
{
int a() { return 1; }
int b() { return 2; }
};

int main()
{
std::vector<std::mem_fun_t<int, MyClass> > vec;
vec.push_back(std::mem_fun(&MyClass::a));
vec.push_back(std::mem_fun(&MyClass::b));
MyClass x;
for (size_t i = 0; i != vec.size(); ++i) {
std::cout << vec[i](&x) << '\n';
}
}

If you are having problems, read the error message carefully. For example, std::mem_fun can return all sorts of wrappers, depending on what you pass to it.

Or indeed, switch to boost's or C++0x's function.



Edit: With this particular error message, I assume that you are doing something that invokes the default constructor for contained type (e.g resize or specifying the size with the vector's constructor). You can't use those functions.

I really can't see why it would not work, but it's actually a pretty ugly solution. Just take vector<function<ReturnType(MyClass*)>> and be without those issues present in C++03 binders.

mem_fun_t meets the requirements to be stored in a container (it is copy-constructible and assignable), so the answer is yes.

However, it isn't default-constructible or comparable, so there are some things you can't do with a container of them, including:

  • Resizing, unless you provide a value to fill with
  • Constructing with a non-zero size, unless you provide a value to fill with
  • Comparing containers

The error you are seeing comes from trying to either resize, or construct with a size.

Category:c# Views:0 Time:2010-09-03

Related post

  • NHibernate: Best way to map to DateTime a value stored in a non-standard way 2008-12-26

    I am just getting started with NHibernate (for the 15th time it would seem) and am having the following problem. The following table: Table Facility Column FACILITY_ID integer Column NAME varchar2(50) Column MONTH varchar2(5) For whatever reason, mon

  • How can I determine the storing standards, such as UTF-8, for files? 2009-08-10

    The question arises from the reply. How can I change the storing from an ISO-standard to UTF-8? Some details: I used Mac with some ISO-standard. I formatted it, so I cannot know the exact ISO-standard. Now, I use Ubuntu, and I try to switch my Mac-la

  • Should a value always be stored in a database using a specific unit (e.g. meters) or should the unit be settable in an additional field? 2009-08-27

    Should a value always be stored in a database using a specific unit (e.g. meters) or should the unit be settable using an additional field or table? Example 1: Unit defined as part of the field buildings ----------------------------- building_id INT

  • SQL Server stored procedure vs an external dll 2010-08-23

    I am trying to convince someone that using an external DLL to manage sql data is better then using stored procedures. Currently the person I am working with is using vba and calls sql stored procedures to get the complicated data they need from many

  • How to deal with localization? Is there a standard I should follow? 2010-11-23

    I'm trying to make a set of classes to deal with localization (mostly the currency part, but also language). I can imagine how the logic could be, but I don't know what names to use for the different languages and currencies. I have seen there are ma

  • C# SQL Server - Passing a list to a stored procedure 2011-08-17

    I am calling a SQL Server stored procedure from my C# code: using (SqlConnection conn = new SqlConnection(connstring)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("InsertQuerySPROC", conn)) { cmd.CommandType = CommandType.StoredProcedure; v

  • java creating byte array whose size is represented by a long 2009-07-01

    I'm trying to create a byte array whose size is of type long. For example, think of it as: long x = _________; byte[] b = new byte[x]; Apparently you can only specify an int for the size of a byte array. Before anyone asks why I would need a byte arr

  • Performance ConcurrentHashmap vs HashMap 2009-09-04

    How is the performance of ConcurrentHashMap compared to HashMap, especially .get() operation (I'm especially interested for the case of only few items, in the range between maybe 0-5000)? Is there any reason not to use ConcurrentHashMap instead of Ha

  • How to call JMagick from a web application 2009-09-15

    I have a servlet based application for serving stored documents and images. The images were all stored as jpg with standard sizes based on corporate standards at the time the system was designed. Now, the image sizes that users want to see have chang

  • POS software/hardware general questions 2009-09-22

    I'm looking for answers to a few general questions as to how point of sale (POS) software and hardware generally works in brick-and-mortar stores. I realize there will be many edge cases given the sheer number of solutions out there, but I'm looking

  • Which key:value store to use with Python? 2009-10-24

    So I'm looking at various key:value (where value is either strictly a single value or possibly an object) stores for use with Python, and have found a few promising ones. I have no specific requirement as of yet because I am in the evaluation phase.

  • Profiling a Set implementation on 64-bit machines 2009-12-18

    Relevant Information on my system: Core2Duo T6500 gcc (GCC) 4.4.1 20090725 (Red Hat 4.4.1-2) Using the basic set implementation, where each set that is stored is really just the lexicographical order of the set stored, you can use standard bit operat

  • Using array as map value: Cant see the error 2010-04-06

    Im trying to create a map, where the key is an int, and the value is an array int red[3] = {1,0,0}; int green[3] = {0,1,0}; int blue[3] = {0,0,1}; std::map<int, int[3]> colours; colours.insert(std::pair<int,int[3]>(GLUT_LEFT_BUTTON,red));

  • How to create item templates for SQL files, for Visual Studio Database Projects 2010-05-05

    It's possible to define your own custom templates for normal project types, such as templates for C# files, for a Class Library project. What about a 'Database Project' scenario? I would like to define a standard template for adding a stored procedur

  • Add empty row to query results if no results found 2010-07-29

    I'm writing stored procs that are being called by a legacy system. One of the constraints of the legacy system is that there must be at least one row in the single result set returned from the stored proc. The standard is to return a zero in the firs

  • JavaScript: Decimal Values 2010-08-11

    What can I use as a decimal type in JavaScript? It's not supported (0.1 + 0.2 !== 0.3), and I need it for representing exact values in a banking/financial application. See The State and Future of JavaScript for a good read and the dirty details behin

  • Setting default Asp.Net Session memory consumption? 2010-10-20

    Some while ago I found a web page explaining the default size of a session was 2048 kb, the minimum was 1 kb. How can this be adjusted? And if some one has links regarding the subject I would be a happy camper :) Sorry not for mentioning the IIS, but

  • Pointers to objects in a set or in a vector - does it matter? 2010-10-28

    just came a across a situation where I needs to store heap-allocated pointers (to a class B) in an STL container. The class that owns the privately held container (class A) also creates the instances of B. Class A will be able to return a const point

  • Lucene insensitive whitespace analyzer? 2011-01-19

    I am using lucene for searching and with tags i use the whitespace analyzer. It looks like its stored properly. With standard analyzer my 'C#' search will yield results for C, C++. Every analyzer i tried (i havent tried all) does this except for whit

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.260 (s). 11 q(s)