User Profile

Collapse

Profile Sidebar

Collapse
tavianator
tavianator
Last Activity: Jan 24 '09, 07:36 PM
Joined: Dec 10 '06
Location:
  •  
  • Time
  • Show
  • Source
Clear All
new posts

  • tavianator
    replied to Ambiguous operator overload?
    in C
    But in this case, all A's need to have the same type, no matter what type T was used in its template constructor, or what possibly different type T is subtracted from it.



    Perhaps you meant:
    Code:
     template<class T, class U> 
    A<T> operator-(const A<T>& lhs, U rhs) 
    { return A<T>(); }
    Either way, I can't really do that. But why is it that the original...
    See more | Go to post

    Leave a comment:


  • tavianator
    started a topic Ambiguous operator overload?
    in C

    Ambiguous operator overload?

    I have an old C++ project which I recently tried use after not compiling it for a while, only to find that g++ borked on it. The problem is reducible to this:
    Code:
    class A
    {
    public:
      A() { }
      template <typename T> A(T t) { }
    };
    
    template <typename T>
    A
    operator-(const A& lhs, T rhs)
    { return A(); }
    
    class B
    {
    public:
      B()
    ...
    See more | Go to post

  • tavianator
    replied to read a line with getchar
    in C
    You set i=0 every time the loop executes. i never exceeds one. Then you set str[0] to '\0'. Set i=0 outside the loop.
    See more | Go to post

    Leave a comment:


  • tavianator
    replied to Static initialization order
    in C
    Really? I checked using g++, and it gave me different results depending on the compilation order of foo.cpp and bar.cpp. I managed to solve my problem a better way, though.
    See more | Go to post

    Leave a comment:


  • tavianator
    started a topic Static initialization order
    in C

    Static initialization order

    I realise that the order of initialization of global static objects across multiple translation units is undefined; however, if a function is defined in the same translation unit as a global static, is the global guaranteed to have been initialized by the time the function is called? For example: is the following code safe?

    foo.hpp:
    [code=cpp]
    struct foo { /* ... */ };

    extern foo f;
    extern foo b;...
    See more | Go to post

  • tavianator
    replied to pointer to class with nested class
    in C
    You should have a member of a_class point to an instance of a_class::b_clas s.
    See more | Go to post

    Leave a comment:


  • tavianator
    replied to Template member function error
    in C
    Aha, silly me, I forgot that dependant member templates need to be prefixed with "template" just like how dependant names have to be prefaced with "typename". This should work (haven't tried it, though):

    Code:
    template <typename T>
    class foo
    {
    public:
      template <typename U> void bar() { }
    };
    
    template <typename T>
    void baz() {
    ...
    See more | Go to post

    Leave a comment:


  • tavianator
    replied to Template member function error
    in C
    It can't possibly matter what goes in a header file and what doesn't, can it? #including happens in preprocessing, before compilation anyway. To be thorough, I tried it that way anyway, and it still doesn't work. It sounds like a compiler bug. Luckily I have a workaround for now.
    See more | Go to post

    Leave a comment:


  • tavianator
    replied to Incompatible type conversion
    in C
    I would suggest using a structure or a class to store information about the product, rather than reams of individual variables. Simply pass an instance of this struct or class by non-const reference to the function which reads the data from the file, or better yet, make that function a member function of the new class.
    See more | Go to post

    Leave a comment:


  • tavianator
    started a topic Template member function error
    in C

    Template member function error

    G++ 4.2.2 reports an error with this innocuous-looking code. Any ideas?

    Code:
    template <typename T>
    class foo
    {
    public:
      template <typename U> void bar() { }
    };
    
    template <typename T>
    void baz() {
      foo<T> f;
      f.bar<T>();
    }
    See more | Go to post

  • tavianator
    replied to Name lookup question
    in C
    Thanks. I forgot about C++'s name hiding rules for a second....
    See more | Go to post

    Leave a comment:


  • I'm pretty sure you switched "re: Ganon" and "re: tavianator".

    Anyway, the following code compiles fine with g++ 4.2.2. Some old compilers handle typename incorrectly, and that may be why the error persisted when you put typename List<T>::ListNo de *List<T>::find( ). Make sure you write it that way in all cases.

    Code:
    template <typename T>
    class List
    {
      public:
    ...
    See more | Go to post

    Leave a comment:


  • Actually, List<T>::ListNo de is a dependent name, so typename is required. Declare the functions like this:

    Code:
    template <typename T>
    typename List<T>::ListNode* List<T>::find()
    etc.
    See more | Go to post

    Leave a comment:


  • Actually, ListNode is a nontemplate, nested struct. Your function should return a List<T>::ListNo de*. So

    Code:
    template <typename T>
    List<T>::ListNode* List<T>::foo();
    when declaring member functions inside classes, however, the List<T>:: is unnecessary.
    See more | Go to post

    Leave a comment:


  • tavianator
    replied to Name lookup question
    in C
    Yeah, I know I could replace foo with func, as you have done. I could also qualify the call ::foo(1). But what in the standard prevents me from doing it the way I did in the original post?
    See more | Go to post

    Leave a comment:


  • tavianator
    started a topic Name lookup question
    in C

    Name lookup question

    The following code seems pretty clear to me, but g++ 4.2.2 gives an error. Am I missing something?

    Code:
    #include <iostream>
    
    void foo(int x) { std::cout << x << std::endl; }
    
    class bar
    {
    public:
      void foo() { foo(1); }
    };
    
    int main() {
      bar baz;
      baz.foo();
    }
    g++ gives

    foo.cpp: In...
    See more | Go to post

  • tavianator
    started a topic Multiple Template-Parameter Friends
    in C

    Multiple Template-Parameter Friends

    I have a problem, which boils down to this:

    Code:
    template<typename T>
    class bar;
    
    template<typename T, typename U>
    void foo(const U& u) {
      bar<T> baz;
      baz.m_member = 0;
    }
    
    template<typename T>
    class bar
    {
      // friend...
    
    private:
      int m_member;
    };
    
    int main() {
      foo<char>(1);
    ...
    See more | Go to post

  • tavianator
    replied to Function Compile Error
    in C
    This code should me correct (note the [2] in the second line, replacing [1]), as long as forces is a two-dimensional array. Where specifically, in the function, is the error?

    Code:
    int initcreate() {
    	int initiative[(MAXUNITS-1)][2];
    	int unitcount = 0;
    	for (int i = 0; i < (BOARDX-1); i++) {
    		for (int j = 0; j < (BOARDY-1); j++) {
    			if (forces[i][j] != 0) {
    				initiative[unitcount][0]
    ...
    See more | Go to post

    Leave a comment:


  • tavianator
    replied to Template argument deduction
    in C
    I figured it out, so I am posting it here in case anyone else stumbles accross the same problem I had. I believe that std::tr1::tuple (and boost::tuple) use this technique.

    Create a struct like this:
    Code:
    template <typename t> struct addcref
    {
      typedef const t& type;
    };
    
    template <typename t> struct addcref <t&>
    {
      typedef t& type;
    };
    ...
    See more | Go to post

    Leave a comment:


  • tavianator
    replied to Incrementing a pointer
    in C
    Do wt_.begin() and wt_.end() return iterators? If so, that should work.
    See more | Go to post

    Leave a comment:

No activity results to display
Show More
Working...