Re: approaches for embedding a data language in a general purpose language
Date: 12 Oct 2006 10:34:45 -0700
Message-ID: <1160674485.695005.197710_at_m73g2000cwd.googlegroups.com>
Marshall a écrit :
> On Oct 10, 1:13 pm, mishad_w..._at_hotmail.com wrote:
> > It _might_ be possible to do that in Managed C++ (I think ISO C++
> > template metaprogramming could provide the type deduction).
>
> Yeah, I used to think it might be possible with C++ too, but it turns
> out it isn't. For the type for the generic natural join, the return
> type
> is a function of the parameter types. C++ can have generic functions
> that have parameterized types, but the resulting types can only be
> simple substitutions of the parameters; it's not possible to generate
> new types as computations on parameter types.
I'm not sure this is not possible in C++. Maybe it is because I don't understand what is a "generic natural join", so allow me to provide an example:
Given two tables:
Employee(EmployeeName, DepartmentName)
Department(DepartmentName, ManagerName)
The natural join of Employee and Department would have the following
columns:
(EmployeeName, DepartmentName, ManagerName)
The goal would be, IIUC, to compute this using C++ template
metaprogramming.
Just for fun, here's an attempt at this, using the Boost.MPL library
(http://www.boost.org/libs/mpl/doc/index.html)
//Declare types for identifying the columns
struct EmployeeName;
struct DepartmentName;
struct ManagerName;
using namespace boost::mpl;
// Define the Employee and Department tables typedef vector<EmployeeName, DepartmentName> EmployeeTable; typedef vector<DepartmentName, ManagerName> DepartmentTable;
// The Joined table's columns list is the union of those from the // Employee and Department tables, with the duplicates removed typedef copy_if<
DepartmentTable,
not_<contains<EmployeeTable, _1> >,
back_inserter<EmployeeTable>
>::type JoinedTable;
// This will not compile if JoinedTable is different from Expected
BOOST_MPL_ASSERT((
equal<
JoinedTable, vector<EmployeeName, DepartmentName, ManagerName>>
)); Received on Thu Oct 12 2006 - 19:34:45 CEST