Search Results

Search found 976 results on 40 pages for 'typedef'.

Page 1/40 | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Proper use of of typedef in C++

    - by dicroce
    I have coworkers who occasionally use typedef to avoid typing. For example: typedef std::list<Foobar> FoobarList; ... FoobarList GetFoobars(); Personally, I always hate coming across code like this, largely because it forces me to go lookup the typedef so I can tell how to use it. I also feel like this sort of thing is a potential slippery slope... If you do this, why aren't you doing it more? (pretty soon, your code is totally obfuscated). I found this SO question regarding this issue: when should I use typedef in C I have two questions: 1) am I truly alone in disliking this? 2) If the vast majority of people think this sort of typedef use is OK, what criteria do you use to determine whether to typedef a type?

    Read the article

  • typedef declaration syntax

    - by mt_serg
    Some days ago I looked at boost sources and found interesting typedef. There is a code from "boost\detail\none_t.hpp": namespace boost { namespace detail { struct none_helper{}; typedef int none_helper::*none_t ; } // namespace detail } // namespace boost I didn't see syntax like that earlier and can't explain the sense of that. This typedef introduces name "none_t" as pointer to int in boost::detail namespace. What the syntax is? And what difference between "typedef int none_helper::*none_t" and for example "typedef int *none_t" ?

    Read the article

  • C++ strongly typed typedef

    - by Kian
    I've been trying to think of a way of declaring strongly typed typedefs, to catch a certain class of bugs in the compilation stage. It's often the case that I'll typedef an int into several types of ids, or a vector to position or velocity: typedef int EntityID; typedef int ModelID; typedef Vector3 Position; typedef Vector3 Velocity; This can make the intent of code more clear, but after a long night of coding one might make silly mistakes like comparing different kinds of ids, or adding a position to a velocity perhaps. EntityID eID; ModelID mID; if ( eID == mID ) // <- Compiler sees nothing wrong { /*bug*/ } Position p; Velocity v; Position newP = p + v; // bug, meant p + v*s but compiler sees nothing wrong Unfortunately, suggestions I've found for strongly typed typedefs include using boost, which at least for me isn't a possibility (I do have c++11 at least). So after a bit of thinking, I came upon this idea, and wanted to run it by someone. First, you declare the base type as a template. The template parameter isn't used for anything in the definition, however: template < typename T > class IDType { unsigned int m_id; public: IDType( unsigned int const& i_id ): m_id {i_id} {}; friend bool operator==<T>( IDType<T> const& i_lhs, IDType<T> const& i_rhs ); }; Friend functions actually need to be forward declared before the class definition, which requires a forward declaration of the template class. We then define all the members for the base type, just remembering that it's a template class. Finally, when we want to use it, we typedef it as: class EntityT; typedef IDType<EntityT> EntityID; class ModelT; typedef IDType<ModelT> ModelID; The types are now entirely separate. Functions that take an EntityID will throw a compiler error if you try to feed them a ModelID instead, for example. Aside from having to declare the base types as templates, with the issues that entails, it's also fairly compact. I was hoping anyone had comments or critiques about this idea? One issue that came to mind while writing this, in the case of positions and velocities for example, would be that I can't convert between types as freely as before. Where before multiplying a vector by a scalar would give another vector, so I could do: typedef float Time; typedef Vector3 Position; typedef Vector3 Velocity; Time t = 1.0f; Position p = { 0.0f }; Velocity v = { 1.0f, 0.0f, 0.0f }; Position newP = p + v*t; With my strongly typed typedef I'd have to tell the compiler that multypling a Velocity by a Time results in a Position. class TimeT; typedef Float<TimeT> Time; class PositionT; typedef Vector3<PositionT> Position; class VelocityT; typedef Vector3<VelocityT> Velocity; Time t = 1.0f; Position p = { 0.0f }; Velocity v = { 1.0f, 0.0f, 0.0f }; Position newP = p + v*t; // Compiler error To solve this, I think I'd have to specialize every conversion explicitly, which can be kind of a bother. On the other hand, this limitation can help prevent other kinds of errors (say, multiplying a Velocity by a Distance, perhaps, which wouldn't make sense in this domain). So I'm torn, and wondering if people have any opinions on my original issue, or my approach to solving it.

    Read the article

  • GDB doesnt like my typedef

    - by yan bellavance
    It seems that the following is to deep for the debugger in Qt even though the program uses it without problem typedef QMap <int, QStringList> day2FileNameType; typedef QMap <int, day2FileNameType> month2day2FileNameType; typedef QMap <int, month2day2FileNameType> year2month2day2FileNameType; year2month2day2FileNameType y2m2d2f; now the first 2 typeDefs work okay with the debugger but the third one retrieving data for watch view (over 100 pending request) Is it wrong for me to try and use such a typedef as year2month2day2FileNameType?

    Read the article

  • C typedef struct uncertainty.

    - by Emanuel Ey
    Consider the following typedef struct in C: 21:typedef struct source{ 22: double ds; //ray step 23: double rx,zx; //source coords 24: double rbox1, rbox2; //the box that limits the range of the rays 25: double freqx; //source frequency 26: int64_t nThetas; //number of launching angles 27: double theta1, thetaN; //first and last launching angle 28:}source_t; I get the error: globals.h:21: error: redefinition of 'struct source' globals.h:28: error: conflicting types for 'source_t' globals.h:28: note: previous declaration of 'source_t' was here I've tried using other formats for this definition: struct source{ ... }; typedef struct source source_t; and typedef struct{ ... }source_t; Which both return the same error. Why does this happen? it looks perfectly right to me.

    Read the article

  • Extracting pair member in lambda expressions and template typedef idiom

    - by Nazgob
    Hi, I have some complex types here so I decided to use nifty trick to have typedef on templated types. Then I have a class some_container that has a container as a member. Container is a vector of pairs composed of element and vector. I want to write std::find_if algorithm with lambda expression to find element that have certain value. To get the value I have to call first on pair and then get value from element. Below my std::find_if there is normal loop that does the trick. My lambda fails to compile. How to access value inside element which is inside pair? I use g++ 4.4+ and VS 2010 and I want to stick to boost lambda for now. #include <vector> #include <algorithm> #include <boost\lambda\lambda.hpp> #include <boost\lambda\bind.hpp> template<typename T> class element { public: T value; }; template<typename T> class element_vector_pair // idiom to have templated typedef { public: typedef std::pair<element<T>, std::vector<T> > type; }; template<typename T> class vector_containter // idiom to have templated typedef { public: typedef std::vector<typename element_vector_pair<T>::type > type; }; template<typename T> bool operator==(const typename element_vector_pair<T>::type & lhs, const typename element_vector_pair<T>::type & rhs) { return lhs.first.value == rhs.first.value; } template<typename T> class some_container { public: element<T> get_element(const T& value) const { std::find_if(container.begin(), container.end(), bind(&typename vector_containter<T>::type::value_type::first::value, boost::lambda::_1) == value); /*for(size_t i = 0; i < container.size(); ++i) { if(container.at(i).first.value == value) { return container.at(i); } }*/ return element<T>(); //whatever } protected: typename vector_containter<T>::type container; }; int main() { some_container<int> s; s.get_element(5); return 0; }

    Read the article

  • Internal typedef and circular dependency

    - by bcr
    I have two classes whose functions take typedefed pointers to eachother as return values and parameters. I.e.: class Segment; class Location : public Fwk::NamedInterface { public: // ===== Class Typedefs ===== typedef Fwk::Ptr<Location const> PtrConst; typedef Fwk::Ptr<Location> Ptr; // ===== Class Typedefs End ===== void segmentIs(Segment::Ptr seg); /* ... */ } and class Location; class Segment : public Fwk::NamedInterface { public: // ===== Class Typedefs ===== typedef Fwk::Ptr<Segment const> PtrConst; typedef Fwk::Ptr<Segment> Ptr; // ===== Class Typedefs End ===== void locationIs(Location::Ptr seg); /* ... */ } This understandably generated linker errors...which the forward declarations of the respective classes don't fix. How can I forward declare the Ptr and PtrConst typedefs while keeping these typedefs internal to the class (i.e. I would like to write Location::Ptr to refer to the location pointer type)? Thanks folks!

    Read the article

  • typedef of a template with a template type as its parameter

    - by bryan sammon
    Im having a problem with a typedef below, I can seem to get it right: template <typename T> struct myclass1 { static const int member1 = T::GetSomeInt(); }; template <int I> struct myclass2 { typedef myclass1< myclass2<I> > anotherclass; static int GetSomeInt(); }; anotherclass MyObj1; // ERROR here not instantiating the class When I try and initialize a anotherclass object, it gives me an error. Any idea what I am doing wrong? There seems to be a problem with my typedef. Any help is appreciated, Thanks Bryan

    Read the article

  • typedef resolution rule

    - by kumar_m_kiran
    Hi All, Can you Please tell me the resolution rule involved in resolving the meaning of the variable in a typedef. Any link related to the same will be very useful. Example #typedef string* pstring; const pstring parr; Here confusion arises whether const'ness is for pointer or the content. Now based on what thumb rule do can we start resolving the above interpretation of the pstring?. Smilarly, If I have a very complex typedef'ed variable, like #typedef void (func*)()(int), I should be able to resolve it using the thumb rule. Thanks in advance for your suggestions

    Read the article

  • typedef to store pointers in C

    - by seriouslion
    The Size of pointer depends on the arch of the machine. So sizeof(int*)=sizeof(int) or sizeof(int*)=sizeof(long int) I want to have a custom data type which is either int or long int depending on the size of pointer. I tried to use macro #if, but the condition for macros does not allow sizeof operator. Also when using if-else, typedef is limited to the scope of if. if((sizeof(int)==sizeof(int *)){ typedef int ptrtype; } else{ typedef long int ptrtype; } //ptrtype not avialble here Is there any way to define ptrtype globally?

    Read the article

  • C++ -- typedef "inside" template arguments?

    - by redmoskito
    Imagine I have a template function like this: template<Iterator> void myfunc(Iterator a, Iterator::value_type b) { ... } Is there a way to declare a typedef for Iterator::valuetype that I can use in the function signature? For example: template< typename Iterator, typedef Iterator::value_type type> void myfunc(Iterator a, type b) { ... } Thus far, I've resorted to using default template arguments and Boost concept checking to ensure the default is always used: template< typename Iterator, typename type = Iterator::value_type > void myfunc(Iterator a, type b) { BOOST_STATIC_ASSERT(( boost::type_traits::is_same< typename Iterator::value_type, type >::value )); ... } ...but it would be nice if there was support in the language for this type of thing.

    Read the article

  • Accessing typedef from the instance

    - by piotr
    As in stl containers, why can't we access a typedef inside the class from the class instance? Is there a particular insight into this? When value_type was a template parameter it could help making more general code if there wasn't the need to specify the template parameters as in vector::value_type Example: class T { public: typedef int value_type; value_type i; }; T t; T::value_type i; // ok t.value_type i; // won't work

    Read the article

  • C++ template typedef

    - by Notinlist
    I have a class template<size_t N, size_t M> class Matrix { // .... }; I want to make a typedef which creates a Vector (column vector) which is equivalent to a Matrix with sizes N and 1. Something like that: typedef Matrix<N,1> Vector<N>; Which produces compile error. The following creates something similar, but not exactly what I want: template <int N> class Vector: public Matrix<N,1> { }; Is there a solution or a not too expensive workaround / best-practice for it? Thanks in advance!

    Read the article

  • How to check if the internal typedef struct of a typedef struct is NULL ?

    - by watchloop
    typedef struct { uint32 item1; uint32 item2; uint32 item3; uint32 item4; <some_other_typedef struct> *table; } Inner_t; typedef struct { Inner_t tableA; Inner_t tableB; } Outer_t; Outer_t outer_instance = { {NULL}, { 0, 1, 2, 3, table_defined_somewhere_else, } }; My question is how to check if tableA is NULL just like the case for outer_instance. It tried: if ( tmp->tableA == NULL ). I get "error: invalid operands to binary =="

    Read the article

  • C Struct : typedef Doubt !

    - by Mahesh
    In the given code snippet, I expected the error symbol Record not found. But it compiled and ran fine on Visual Studio 2010 Compiler. I ran it as a C program from Visual Studio 2010 Command Prompt in the manner - cl Record.c Record Now the doubt is, doesn't typedef check for symbols ? Does it work more like a forward declaration ? #include "stdio.h" #include "conio.h" typedef struct Record R; struct Record { int a; }; int main() { R obj = {10}; getch(); return 0; }

    Read the article

  • C++ typedef for partial templates

    - by Gokul
    Hi, i need to do a typedef like this. template< class A, class B, class C > class X { }; template< class B, class C > typedef X< std::vector<B>, B, C > Y; I just found that it is not supported in C++. Can someone advise me on how to achieve the same through alternative means? Thanks, Gokul.

    Read the article

  • typedef fixed length array

    - by Rajorshi
    Hi, I have to define a 24-bit data type.I am using char[3] to represent the type. Can I typedef char[3] to type24? I tried it in a code sample. I put typedef char[3] type42; in my header file. The compiler did not complain about it. But when I defined a function void foo(type24 val) {} in my C file, it did complain. I would like to be able to define functions like type24_to_int32(type24 val) instead of type24_to_int32(char value[3]).

    Read the article

  • Typedef and Struct in C and H files

    - by Leif Andersen
    I've been using the following code to create various struct, but only give people outside of the C file a pointer to it. (Yes, I know that they could potentially mess around with it, so it's not entirely like the private keyword in Java, but that's okay with me). Anyway, I've been using the following code, and I looked at it today, and I'm really surprised that it's actually working, can anyone explain why this is? In my C file, I create my struct, but don't give it a tag in the typedef namespace: struct LABall { int x; int y; int radius; Vector velocity; }; And in the H file, I put this: typedef struct LABall* LABall; I am obviously using #import "LABall.h" in the c file, but I am NOT using #import "LABall.c" in the header file, as that would defeat the whole purpose of a separate header file. So, why am I able to create a pointer to the LABall* struct in the H file when I haven't actually included it? Does it have something to do with the struct namespace working accross files, even when one file is in no way linked to another? Thank you.

    Read the article

  • typedef a functions prototype

    - by bitmask
    I have a series of functions with the same prototype, say int func1(int a, int b) { // ... } int func2(int a, int b) { // ... } // ... Now, I want to simplify their definition and declaration. Of course I could use a macro like that: #define SP_FUNC(name) int name(int a, int b) But I'd like to keep it in C, so I tried to use the storage specifier typedef for this: typedef int SpFunc(int a, int b); This seems to work fine for the declaration: SpFunc func1; // compiles but not for the definition: SpFunc func1 { // ... } which gives me the following error: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token Is there a way to do this correctly or is it impossible? To my understanding of C this should work, but it doesn't. Why? Note, gcc understands what I am trying to do, because, if I write SpFunc func1 = { /* ... */ } it tells me error: function 'func1' is initialized like a variable Which means that gcc understands that SpFunc is a function type.

    Read the article

  • typedef struct, circular dependency, forward definitions

    - by BlueChip
    The problem I have is a circular dependency issue in C header files ...Having looked around I suspect the solution will have something to do with Forward Definitions, but although there are many similar problems listed, none seem to offer the information I require to resolve this one... I have the following 5 source files: // fwd1.h #ifndef __FWD1_H #define __FWD1_H #include "fwd2.h" typedef struct Fwd1 { Fwd2 *f; } Fwd1; void fwd1 (Fwd1 *f1, Fwd2 *f2) ; #endif // __FWD1_H . // fwd1.c #include "fwd1.h" #include "fwd2.h" void fwd1 (Fwd1 *f1, Fwd2 *f2) { return; } . // fwd2.h #ifndef __FWD2_H #define __FWD2_H #include "fwd1.h" typedef struct Fwd2 { Fwd1 *f; } Fwd2; void fwd2 (Fwd1 *f1, Fwd2 *f2) ; #endif // __FWD2_H . // fwd2.c #include "fwd1.h" #include "fwd2.h" void fwd2 (Fwd1 *f1, Fwd2 *f2) { return; } . // fwdMain.c #include "fwd1.h" #include "fwd2.h" int main (int argc, char** argv, char** env) { Fwd1 *f1 = (Fwd1*)0; Fwd2 *f2 = (Fwd2*)0; fwd1(f1, f2); fwd2(f1, f2); return 0; } Which I am compiling with the command: gcc fwdMain.c fwd1.c fwd2.c -o fwd -Wall I have tried several ideas to resolve the compile errors, but have only managed to replace the errors with other errors ...How do I resolve the circular dependency issue with the least changes to my code? ...Ideally, as a matter of coding style, I would like to avoid putting the word "struct" all over my code.

    Read the article

  • Why should structure names have a typedef?

    - by Jay
    I have seen source codes always having a typedef for a structure and using the same everywhere instead of using the structure name as "struct sname" etc directly? What is the reason behind this? Are there any advantages in doing this?

    Read the article

1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >