ANN: new release of fructose with support for hamcrest matchers
This is to announce a new release of FRUCTOSE, a C++ unit test
framework. It is very cutdown compared to CppUnit and is designed for
simple command line-driven test harnesses. It is implemented entirely
in header files. Try it out at http://fructose.sourceforge.net. This
is release 0.9.0. It has support for hamcrest matchers via
fructose_assert_that, and a couple of minor bug fixes.
Regards,
Andrew Marlow
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/29/2010 10:38:16 PM
|
0
|
Andrew <marlow.and...@googlemail.com>
|
|
|
Passing "empty" types as parameters
Hello,
What is the recommended way to pass "empty" types as parameters? By
empty I mean that they have no fields (or methods, typically), thus
really serving as placeholders (e.g. mpl::void_, null_type or even
none_t.)
Is it better to accept them as values, const values or const
references? E.g.
void foo(void_);
void foo(void_ const);
void foo(void_ const&);
By "better" I mean in terms of both semantics (making it clear to
readers that it's a placeholder) and optimization (since I'm not sure
which one is usually easier for a smart compiler to eliminate
entirely.)
Thanks
|
8/29/2010 10:34:21 PM
|
1
|
AJG <plus....@gmail.com>
|
problems with polymorphism and inheritance with templates
This code:
template<class T>
class Foo
{
public:
T* member;
};
class Base
{
};
typedef Foo<Base> BaseFoo;
class Derived
{
};
typedef Foo<Derived> DerivedFoo;
int main(int argc, char** argv)
{
DerivedFoo* dfoo = new DerivedFoo();
BaseFoo* bfoo = dfoo;
return 0;
}
doesn't compile, the compiler says "cannot convert �DerivedFoo*� to
�BaseFoo*� in initialization". How can I fix it? Of course, this one works:
Derived* derived = new Derived();
Base* base = derived;
I know that something similiar (but not really the same) is possible,
because share
|
8/29/2010 9:47:28 AM
|
2
|
Frank Buss <...@frank-buss.de>
|
Undefined behavior on integer literals overflow ?
Hello
I have been reading through the C++ standard and I am surprised to see that users ought to make sure that integer literals never overflow, before compiling their program, as an integer literal overflow results in undefined behavior !
On the other hand, the size of a long int is supposed to be just possibly larger than an int, which in turn is meant to be the native size supported by the underlaying hardware.
In other words, you do not really know the size of a long it, but if some literal overflows it, BOOM !
Boy this looks so primitive to me ! And to defy reason even more, i
|
8/29/2010 9:46:18 AM
|
2
|
Timothy Madden <terminato...@gmail.com>
|
std::accum w/ unary op
I just had quite a confusing bout with my compiler, until I realized
that the following wasn't part of the standard:
namespace nonstd {
// an accumulate which follows the standard accumulate, except
// instead of:
// result = result + *i
// at each iteration, it performs:
// result = result + f(*i)
// i.e. it applies a unary op to the item iterated over.
template<class InputIter, typename T, class UnaryFunc>
T accumulate(InputIter first, InputIter last, T init,
UnaryFunc uop)
{
T rv = init;
f
|
8/28/2010 2:40:51 PM
|
2
|
tf <...@sci.utah.edu>
|
const static data member from abstract class crashes my program
Hi All,
Thanks in advance for any help / insight on this matter.
1) This piece of code (inside the class ReassignCaseRequestHandler :
public CaseRequestHandler implementation file) calls a const static
data member (CaseRequestHandler::PARAM_CALLING_TRANSACTION) (see code
below). The problem I am having is that this static data member (first
param to MessageParameter) is uninitialized (bss section).
Platform: CentOS 5
gcc: version 4.1.2 20080704 (Red Hat 4.1.2-48)
const MessageParameter*
ReassignCaseRequestHandler::requestParameters1[] =
{
new
MessageParameter( CaseRequestH
|
8/26/2010 9:06:05 PM
|
3
|
sil <schit...@gmail.com>
|
Declaring a template class with two template params a friend in a non-template class
I have an abstract class:
class Attribute { /* ...*/ };
Now I have another class:
template <typename T, typename U>
class Mgr
{
static U *create(const std::string k, std::string v)
{
static T *p = 0;
if (!p)
{
p = new T();
}
T &r = *p;
U *a = const_cast<U*>(r.get(k));
return a;
}
};
Both of these classes are declared/defined in different header files.
The problem is that the constructors of Attribute class are private so
I have to declare the Mgr class a friend of Attribute. That's wha
|
8/25/2010 11:22:34 AM
|
1
|
A L <alapex0...@gmail.com>
|
assignment of std::valarray
Hi all.
Im trying to parse the C++0x documentation related to the assignment
of std::valarray.
This code is tested under g++ 4.4.3, VisualStudio2008 and finally
VS2010.
Three examples of assigning one valarray to another:
1. Pure assignment
std::valarray<int> a(2), b(2);
std::cerr << &(a[0]) << std::endl;
a = b;
std::cerr << &(a[0]) << std::endl;
2. Negation + assignment
std::cerr << &(a[0]) << std::endl;
a = -b;
std::cerr << &(a[0]) << std::endl;
3. Computed assignment:
std::cerr << &(a[0]) << std::endl;
a -= b;
std::cerr << &(a[0]) << std::endl;
g
|
8/25/2010 11:28:22 AM
|
0
|
backminator <backmina...@gmail.com>
|
Assigning value of unassigned iterator
Hi all.
I've been running c++ code under Linux gcc 2.93->4.4.3, OSX, and
Windows (VisualStudio 2005-2008) for > 3 years.
Now we are upgrading to VS2010 which supports C++00. We now suddenly
got a problem related to iterators.
The question is really, is the code below valid or not? It runs ok on
every instance of a compiler I can get my hands on, except VS2010.
It crasches on the assignment of the iterator, where it is trying to
dereference a null pointer.
My question is not really about this and that implementation of
compiler, its more of a C++, is this ok question.
Using unass
|
8/24/2010 3:00:22 PM
|
5
|
backminator <backmina...@gmail.com>
|
"Dependent" non-typename template arguments
Is something like the following allowed?
template <typename T, T V>
struct foo {};
foo<bool, true>
MSVC accepts it happily, but that's never a reliable indicator.
What about:
template <typename T>
struct foo {
template <T V>
struct bar {};
};
foo<bool>::bar<true>
Thanks.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/24/2010 3:02:47 PM
|
2
|
AJG <plus....@gmail.com>
|
Integral promotions for char16_t, char32_t, and wchar_t
According to N3092 - 4.5/2,
"A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be
converted to a prvalue of the first of the following types that can
represent all the values of its underlying type: int, unsigned int,
long int, unsigned long int, long long int, or unsigned long long int.
If none of the types in that list can represent all the values of its
underlying type, a prvalue of type char16_t, char32_t, or wchar_t can
be converted to a prvalue of its underlying type."
I can't imagine an implementation where 'unsigned int' cannot
represent all values of 'char16_t'
|
8/24/2010 12:08:54 AM
|
1
|
Nikolay Ivchenkov <ts...@mail.ru>
|
returning a std::auto_ptr, a question of style
Hello,
I am using a class that returns a pointer to memory that I have to
take charge of. I will assign it to a std::auto_ptr so the
deallocation is taken care of. My question is, would the function I am
calling be better if it was declared to return a std::auto_ptr, rather
than a naked pointer? That way it would be self-documenting and I
couldn't accidently forget to assign it to a std::auto_ptr.
An example of where I am being passed a pointer to memory that I have
to take charge of is a virtual ctor that creates an object read from a
message queue. The message is in XML (FWIW).
|
8/23/2010 11:58:24 AM
|
15
|
Andrew <marlow.and...@googlemail.com>
|
best code browser
i am searching for some good c++ code browser, i have used visual
slick it was good but looking for something like ecb (emac code
browser) i tried ecb but couldnt find some useful tutorials for using
it efficiently.
if anyone can suggest me on this, it would be helpful.
thanks
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/22/2010 7:03:14 PM
|
1
|
blackbug <blackbug....@gmail.com>
|
Should C++0x contain a distinct type for UTF-8?
Hi I have posted this question to c.std.c++ a few weeks back but failed
to get any useful response.
So I thought I'd try a second time:
Should C++0x contain a distinct type for UTF-8?
Current draft N3092 specifies:
+ char16_t* for UTF-16
+ char32_t* for UTF-32
+ char* for execution narrow-character set
+ wchar_t* for execution wide-character set
+ unsigned char*, possibly for raw data buffers etc.
a) Wouldn't it make sense to have a char8_t where char8_t arrays would
hold UTF-8 character sequences exclusively?
b) What is the rationale for not including it?
cheers,
Mart
|
8/22/2010 2:15:05 PM
|
27
|
"Martin B." <0xCDCDC...@gmx.at>
|
Linkage confusion with "const" variabes
I have a few questions about the linkage from the following variables. By
examples of 7.1.1/7 of C++03 and experimenting with compilers, I came to the
following linkage kinds:
#1
static int a; #a
extern int a; #b // valid, 'a' still internal
#2
extern int b; #c
static int b; #d // invalid!
#3
const double pi1 = 3.14; #e
extern const double pi1; #f // valid and 'pi1' is internal
#4
extern const double pi2; #g
const double pi2 = 3.14; #h // valid but 'pi2' is now external
In #1, it's clear to me with accordance to 3.5: #a implies internal linkage.
And #b also impli
|
8/21/2010 8:14:06 PM
|
1
|
"Johannes Schaub (litb)" <schaub-johan...@web.de>
|
Any news?
Hi @all,
I just wanted to know if there is any news on this topic. In my
opinion, cv-qualified constructors would be a nice feature (which has
no real workaround). I was hoping for the C++0x standard, but I am
very disappointed that I havn't found this issue browsing through the
draft. Have I overlooked something? Is this issue still in discussion?
Maybe an insider (standardization process) could comment on my
question.
Thank you very much!
Mekiro
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: D
|
8/21/2010 10:06:44 AM
|
4
|
mek...@thankyou2010.com
|
why allow class and typedef to use the same name?
We know both class and typedef declare types. I do not understand why
the same name could be used for both a class and a typedef-name---at
least in gcc v4.4.0---as in the following example.
//--------------------------------------
struct X {
struct A {};
typedef int A; // why not conflict?
A m; // why resolve to 'typedef A'?
};
int main() {
X x;
x.m = 1.0; // ok
X::A y; // why resolve to 'struct A'?
y = 1.0; // error
return 0;
}
//----------------------------------------
Thank for your help,
Jason
--
[ See http://
|
8/19/2010 8:37:26 PM
|
2
|
Jia-sen <baowei2...@gmail.com>
|
argument packs manipulations
Hello,
I am trying to make a class that acts like the following, but in any
dimension:
template <typename T>
struct Vec3 {
T tab[3];
Vec3(T const& t1,T const& t2):tab({t1,t2,1}){}
Vec3(T const& t1,T const& t2,T const& t3):tab({t1,t2,t3}){}
Vec3(T const& t1,T const& t2,T const& t3,T const& t4)
:tab({t1/t4,t2/t4,t3/t4}) {}
};
My first try is:
template <typename T,int d>
struct Vec {
T tab[d];
template <class... U>
Vec(U... u):tab({u...}){
if(sizeof...(u)==d-1) tab[d-1]=1;
|
8/19/2010 1:14:06 PM
|
4
|
Marc <marc.gli...@gmail.com>
|
Output order for cout << "Hello, " << "world!" ?
Hello
Is it true that the language does not actually guarantee the output
order for
cout << "Hello, " << "world.!" << endl;
like it could possibly output "world !\nHello" or the like ?
Because there is no semicolon ";" to introduce a sequence point, until
the end of the expression, and until then, the order of all side-effects
unspecified ?
I have tried to understand this and it occur to me that the above
expression is really a function-call expression, and should follow the
semantics of a function call, like
operator <<
(
operator <<(operator <<(cout, "Hel
|
8/19/2010 1:15:40 PM
|
4
|
Timothy Madden <terminato...@gmail.com>
|
functions vs. static member functions
Hello,
I'm trying to find out, if the following two codes are equivalent:
// 1
class foo {
public:
static int val;
static void bar() { val = 0; }
};
// 2
namespace foo {
int val;
void bar() { val = 0; }
}
It would be cool, if someone could help me.
Regards,
Dobi
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/19/2010 1:03:15 PM
|
7
|
Dobi <ha...@daiw.de>
|
Elaborated type specifier as template argument
Consider the following class:
template <typename T> foo {};
Is used by two translation units:
// TU1
typedef foo<struct incomplete_type> tu1;
// TU2
typedef foo<struct incomplete_type> tu2;
Are tu1 and tu2 guaranteed to refer to distinct types? Why?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/18/2010 6:01:17 PM
|
2
|
GMan <gmanni...@gmail.com>
|
Unable to read the bits of a 'float' using char*
Hello everyone,
I wrote these two functions for reading the bits of a float.
However, I can't figure out why the first method shown below works but
not the second one. I was sure the second one would also work but
that's not the case and I can't seem to find anything wrong with its
implementation.
I am rather puzzled with this. Could someone please shed some light on
this matter?
Thanks in advance,
Victor Freire
// Works OK
std::string getBits(const void* number, size_t sz)
{
unsigned *byte = (unsigned *)number;
std::string bits = "";
for(unsigned b = 0; b <
|
8/17/2010 10:50:21 PM
|
13
|
Victor Freire <victorlunafre...@gmail.com>
|
template function (vector<int> vs. vector<gsl_vector*>)
hi, there,
I have
void Foo1(vector<int>& v){
....
A.process(v[i]);
....
}
void Foo2(vector<gsl_vector*>& v){
....
A.process(v[i]);
....
}
How can I integrate Foo1 and Foo2 into a template function? Thanks.
zl2k
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/17/2010 6:18:24 PM
|
3
|
zl2k <kdsfin...@gmail.com>
|
Sorting by a transformation function
Hi,
I would like to sort a vector ordered by the return value of a unary
transformation function, op. The elements of the vector do not have a
copy constructor. The transformation function is expensive and show be
called exactly once for each element of the vector. I have a solution,
included below. I want to know if this operation is possible using
ether standard functions or using boost, and whether my solution could
be improved at all. For bonus points, I'd like to include an optional
binary predicate as a fourth parameter that can be passed to
std::sort.
Cheers,
Shaun
#in
|
8/17/2010 6:17:10 PM
|
7
|
ShaunJ <sjack...@gmail.com>
|
C++ template compile error on g++ 4.1.2
Can anyone explain to me why the following code doesn't compile (I
stripped it down from a larger multi-file application). Is this a
problem with the compiler or me :-):
/*
* Below is a example showing the problem I'm having trying to compile under
* gcc 4.1.2 on Red Hat Enterprise Linux release 5.3 (Final). I get the
* following compile errors:
*
* What am I doing wrong?
*
* test/TestCase.cpp:97: error: too few template-parameter-lists
* test/TestCase.cpp:102: error: too few template-parameter-lists
*
* Mike
*/
#include <string>
#include <iostream>
namespace
|
8/17/2010 1:57:35 AM
|
1
|
Mike <mh...@bluezoosoftware.com>
|
how do you write proxies for rvalues?
As an exercise to familiarize myself with rvalues I decided to write a
proxy class for plus operator. It turned out to be more difficult than
I thought.
Here is what I got (tell me if I did it wrong):
template<class T1, class T2>
class PlusProxy
{
T1 t1;
T2 t2;
public:
PlusProxy(T1&& t1, T2&& t2)
: t1(std::forward<T1>(t1)),
t2(std::forward<T1>(t2))
{}
PlusProxy(PlusProxy&& other)
: t1(std::forward<T1>(other.t1)),
t2(std::forward<T1>(other.t2))
{}
PlusP
|
8/17/2010 1:56:54 AM
|
3
|
Gene Bushuyev <publicfil...@gbresearch.com>
|
implicit copy/move constructors
I've been testing rvalues in gcc 4.5 and VC 2010 and noticed they both
generated implicit copy constructors even though I provided custom
move constructors, which led to some surprises before I found that. Is
that conforming to the latest C++0x? I kind of expected the other ctor
to be suppressed by providing the user defined copy/move ctor.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/16/2010 12:49:00 PM
|
3
|
Gene Bushuyev <publicfil...@gbresearch.com>
|
Can I have your feedback on this general mix-in pattern?
Hi,
I needed to implement cloning and saw that I ended up with copy and
paste all over the code. I thought that this should be able to be solved
with mix-ins. After research and help from you (Is the Mixin pattern
accepted in all camps? 6 July) I thought that I probably would find a
good solution to this.
I found a nice first approach at Alf P. Steinbach's blog.
http://alfps.wordpress.com/2010/06/12/cppx-3-ways-to-mix-in-a-generic-cloning-implementation/
The approach that caught my interest was Way #2: Mix-in via a middleman
base class.
However, I thought the syntax would b
|
8/15/2010 5:39:58 PM
|
2
|
DeMarcus <use_my_alias_h...@hotmail.com>
|
member function as predicate
{ Unintended linebreaks are removed manually. Please limit your text
to 70 columns or so to prevent unintended linebreaks. -mod }
I've been trying to use find_if() to search a map for an element whose value
is equal to a particular number. I'm trying to use a member function as the
predicate, but I can't get the code to compile. I've attached code that
illustrates the problem. The error I get from g++ is that the mem_fun object
isn't a valid operator for use by bind1st. Can some one point out how to fix this?
I know that I can do this using a global predicate function, a stati
|
8/15/2010 2:19:21 AM
|
2
|
"richardson.t...@gmail.com" <richardson.t...@gmail.com>
|
template function Ambiguity.
I developed class vector and class matrix use mingw gcc.
then move to vs 2008 get Problem:
template<class TYPE,int DIM=3>
class Vector
...........
template<class TYPE,int COL=4,int ROW=4>
class Matrix
..............
when call Vector<double,4>*Matrix<double,4,4>
Ambiguity in
template<class TYPE,int COL,int ROW>
Vector<TYPE,COL> operator*(Vector<TYPE,ROW>&v,Matrix<TYPE,COL,ROW>
const&m)
and
template<class TYPE,int DIM,class SCALETYPE>
Vector<TYPE,DIM> operator*(Vector<TYPE,DIM> const&v,SCALETYPE t)
vs2008 bug or Correct?(mingw gcc is pass).
how can I Solve this and don
|
8/14/2010 5:37:16 PM
|
1
|
FE <blood_...@sina.com.cn>
|
Is this a valid optimisation?
I've seen one compiler where this:
int32_t get_val(char const *s, std::size_t len, uint8_t base)
{
bool is_neg;
// set isneg
int32_t min = std::numeric_limits<int32_t>::min();
int32_t max = std::numeric_limits<int32_t>::max();
int32_t lim = isneg ? (-(min / base)) : (max / base);
//blah
}
Now on one compiler, when you switch on optimisation, the last line
appears to be evaluated as:
int32_t lim = (isneg ? -min : max) / base;
which doesn't work, as -min is still negative, and lim gets a negative
result, whereas it should be positive.
So -
|
8/12/2010 12:13:17 PM
|
8
|
ThosRTanner <ttann...@bloomberg.net>
|
Why does a base function hide a function in a derived class?
It is normal that a function in derived class hides one in a base
class. But, I could explain to myself how a base function could hide
one in a derived class. Here is an example:
struct A { int x(); };
struct B : A { };
struct C : A { };
struct D : B, C {
using A::x;
int x(double);
void f();
};
void D::f() {
x(1.0); //error in gcc v4.4.0, No match for
'A::x(double)'
}
int main() { }
Isn't there a D::x(double)? I would appreciate if someone could
explain the error message.
Thanks,
Jason
--
[ See http://www.gotw.ca/resourc
|
8/12/2010 7:07:00 AM
|
5
|
Jia-sen <baowei2...@gmail.com>
|
STL implementation of list::sort
I was wondering how is list member function sort implemented in STL.
Since list has bidirectional iterator so any of the random access
iterator based sorting algorithms (heap sort, quick sort etc) can not
be applied on lists. Still the complexity of list::sort is NlogN. So
which algorithm does it apply? I tried to look at algorithm.h where
some sort algos use introsort ( a mix of quick and heap sort) , but
still could not get how it is done for list. There is one way which I
can think of the way STL might implement list::sort is that is copies
list elements into a vector , applies an
|
8/12/2010 7:04:44 AM
|
8
|
Awashesh Kumar <awashesh.ku...@gmail.com>
|
Output Generated From Templates
Hi all,
Is there any tool or compiler that will output the result of C++
template instantiation? I think such a thing could be useful for
advanced template code development, and also education.
Cheers,
P.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/12/2010 7:03:19 AM
|
1
|
PGK <graham.k...@gmail.com>
|
Digital Mars C/C++ Compiler: test results
> ----------------------------------alignment------------------------------------
On 9 Aug, 01:48, Walter Bright <newshou...@digitalmars.com> wrote:
> Digital Mars C++ is very fast at compiling, far faster
> than any other C++ compiler.
I have tested free Digital Mars C/C++ Compiler v. 8.52 from
http://www.digitalmars.com/download/freecompiler.html
The results were impressive, so I decided to show here how well this
fastest compiler works.
1)
namespace
{
struct X {};
}
int main()
{
::X x; // well-formed
}
Compiler message:
|
8/12/2010 12:52:28 AM
|
5
|
Nikolay Ivchenkov <ts...@mail.ru>
|
Problem with printing to a file
Hi,
I wrote a program in which the main part is a loop with a big number
of iterations (the program takes about 1 day to perform the
calculations i want) that imitates a flow of time ("professionally
speaking": time evolution). After each iteration of the loop I send
some data to files. To make the long story short, u can simply imagine
a two columns file with time values (proportional to the the loop
iterator) in the 1st column and some number/quantity (say, energy) in
the 2nd column.
So, at the very beginning of the code I open the file:
ofstream EnergyStream;
EnergyStream.ope
|
8/12/2010 12:56:04 AM
|
0
|
megaziomek <okurczebl...@gmail.com>
|
copy constructor elision
class A {
};
class B : public A {
B(const B& other);
public:
explicit B(){}
};
int main() {
A const & a = B(); // L1
A a2 = B(); // L2
}
Why VC++ 2010 and g++ give error related to private copy constructor
in 'B' in Line L2. I understand the Line L1 is governed by $8.5.3/5 (C+
+ 2003)
Dabs.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/12/2010 1:03:54 AM
|
5
|
Anubhav <rkld...@gmail.com>
|
ADL and C++0X Lambdas
Are there supposed to be some strange interactions with C++0X lambdas
and ADL? The following code, works in Intel 11.1 and GCC4.5 but fails
in VisualStudio 2010. Is this a Microsoft bug? If so, I posted it on
https://connect.microsoft.com/VisualStudio/feedback/details/585643/bug-in-adl-lookup-with-c-0x-lambdas#details
if anyone wants to vote for it. Thanks
namespace X
{
struct Y{};
template<typename F>
void myfunc(F f, Y y) {}
}
double identity(double x){return x;}
int main()
{
myfunc(identity, X::Y()); //ADL Works as expected
myfunc([](double x){return
|
8/11/2010 11:27:42 PM
|
2
|
Jesse Perla <jessepe...@gmail.com>
|
Groups down
The groups are experiencing server problems. We expect to be back on line
evening of 12 August.
Messages in queues will be processed. Mailers will likely deliver other
posts shortly after recovery.
When we come back, please allow eight hours before reposting any articles
which have not been processed.
Thanks for your patience,
The mods
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/11/2010 10:04:32 PM
|
0
|
The Moderators <c++-requ...@cs.rpi.edu>
|
Vptr
Hi!
I have another naive question. I have been trying to understand the
polymorphysim implementation in C++.
I believe that most of the compilers implement it using vptr and
vtables.
My question is when for every object of a class (which has a virtual
member) if vptr points to the same vtable, why does every object has
a copy of the same vptr?
For example
say X is a class which has a virtual function
and a and b are objects of that class
both a and b will have a vptr installed in them and this vptr will
point to the same vtable
Why not have the vptr as a static data member of t
|
8/10/2010 2:20:29 PM
|
10
|
MC <manan.cho...@gmail.com>
|
why template version is prefered in this case
#include <iostream>
using namespace std;
template <typename T>
void f(const T a) {
cout<<"Template version."<<endl;
}
void f(const int* a) {
cout<<"Plain version."<<endl;
}
int main() {
int *a=0;
f(a);
return 0;
}
the output is Plain version., I don't understand why is the case
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/10/2010 1:41:57 PM
|
9
|
junvi <junvi.b...@gmail.com>
|
Is there std function that calculates a bounded value?
Is there a boost/std equivalent to:
template< typename rc >
rc Bounded( rc const& rValue, rc const& rLowest, rc const& rHighest )
{
assert( !( rHighest < rLowest ) );
if( rLowest < rValue ) {
if( rValue < rHighest ) {
return rValue;
} else {
return rHighest;
}
} else {
return rLowest;
}
}
For strictly ordered types: if ( a<=b ) then ( Bounded( x, a, b ) ==
min( max( x, a ), b ) )
Also the deduction might be fixed to be based only on the first
argument (but it's not crucial for me).
itaj
--
[ See http://www.gotw.ca/resource
|
8/9/2010 8:51:29 AM
|
1
|
itaj sherman <itajsher...@gmail.com>
|
Shallow copy
Hi I am writing a library in C++. In my library I want to implement a
feature that if an application programmer instantiates a class object
and later does a shallow copy by memcpy or by any other means. During
the run time when ever any function of the shallow copy object is
called it should print out a message Warning: Shallow Copy detected.
What will be the best way to do this?
Thanks.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/7/2010 8:31:20 PM
|
15
|
MC <manan.cho...@gmail.com>
|
Set implementation in STL
How are sets generally implemented in STL. I have read they are
implemented as BST, are they some kind of balanced BST like Red Black
trees?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/7/2010 8:05:07 PM
|
7
|
MC <manan.cho...@gmail.com>
|
output?
int& f(){
static int x = 0;
x++;
return x;
}
int main(){
int x = 0;
f() += 1;
f() = f() + 1;
cout << f();
}
What should be the output of the code shown?
VS gives 5 and gcc gives 6.
Regards,
Dabs.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/7/2010 3:18:04 AM
|
13
|
Anubhav <rkld...@gmail.com>
|
C/C++ query(nested struct)
In case of a nested struct, does the inner struct can access the
private member of outer struct.
struct str1
{
private:
int a;
public:
struct str2
{
public:
void func(void);
};
};
void str1::str2::func()
{
str1 obj;
obj.a=10; //this is working in GNU compiler, but according to
Bruce Eckel this should be an error
}
int main()
{
str1::str2 obj2;
obj2.func();
}
Kindly solve my doubt, this really is reported to be an error in Bruce
Eckel volume 1 but when i run this on my
|
8/6/2010 3:48:35 AM
|
2
|
Abhishek <goluagarw...@gmail.com>
|
C/C++ query
For a const variable, in general the compiler does'nt allocate memory
for that variable in C++. So you cannot reference the variable to
initialize any array for its size as:
const int a=5;
int arr[a];//incorrect
The above code is also verified to be incorrect in Bruce Eckel. But to
my surprise its running well and fine in both GNU compiler and DEV-CPP
compiler.
Please Help.
Regards,
Abhishek
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/6/2010 3:48:35 AM
|
8
|
Abhishek <goluagarw...@gmail.com>
|
C/C++ query
Initializing an array via a variable is restricted in C++.
int a=5;
int arr[a];//error
I checked it in GNU compilers to my astonishment that this code really
works.
Also suppose an array is declared to hold 5 data.i.e.
int arr[5];
Now, if you try to assign value to array element even greater than 5,
the compiler doesnt have a problem.
arr[7]=10;//this works
Kindly solve the doubt as to what is going wrong.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
8/6/2010 3:23:32 AM
|
4
|
Abhishek <goluagarw...@gmail.com>
|
a question of style when catching exceptions
I am working on enhancements and changes to a C++ system that uses
exceptions. A very common way the code catches exceptions is by the
base class, std::exception. I used to think there was nothing wrong
with this but my experience with this codebase is making me have
second thoughts. When I describe the issues, maybe you guys can give
your opinions.
The code uses some byzantine libraries that can throw for one of three
reasons:
1) It really needs to throw, coz there was a runtime error and they do
happen from time to time.
2) it throws std::logic_error because it has detected a s
|
8/5/2010 12:54:36 PM
|
8
|
Andrew <marlow.and...@googlemail.com>
|
Right Endian conversion
Hi to all,
I've written down this function for perfoming endian conversion :
template<typename T>
T swap_endian(const T& source)
{
T ret = 0;
for (int i = 0; i < sizeof(T); ++i)
{
*(reinterpret_cast<char*>(&ret) + i) =
*(reinterpret_cast<char*>(&source) + sizeof(T) - i - 1);
}
return ret;
}
It seems to work the most of the times, but I'm not sure when I use it
on signed types (int long etc etc) with negative numbers or with
positive numbers like 1520 :
bigendian 0x05F0
littleendian 0xF005
Is it correct, or has anyone a better routine ?
|
8/5/2010 1:00:30 PM
|
6
|
Giulio Guarnone <giulio.guarn...@gmail.com>
|