View unanswered posts | View active topics It is currently Fri Dec 15, 2017 5:08 pm



Reply to topic  [ 4 posts ] 
 Template compilation error w GCC 7.1.0 (ambiguous overload) 
Author Message

Joined: Fri Nov 03, 2017 6:56 pm
Posts: 2
I picked up an old GBA project I would like to finish now. I have a template fixed-point class (ideone example) with tests that compiles with GCC 5.4 (my PC) and 6.3 (IdeOne) and even 7.1 (wandbox), but not the 7.1 included in the current devkitArm. I've tried a couple of things now, but can not get it to work.
GCC always tells me that:
Code:
/src/test.cpp: In function 'int main()':
/src/test.cpp:7:13: error: call of overloaded 'fp(int)' is ambiguous
  fp1616 a0(0);
             ^
In file included from /src/test.cpp:1:0:
/src/test.h:10:2: note: candidate: fp<F>::fp(const float&) [with int F = 16]
  fp(const float & b) : s(b * (1 << F)) { }
  ^~
/src/test.h:9:2: note: candidate: fp<F>::fp(const int32_t&) [with int F = 16; int32_t = long int]
  fp(const int32_t & b) : s(b << F) { }
  ^~
/src/test.h:4:8: note: candidate: constexpr fp<16>::fp(const fp<16>&)
 struct fp
        ^~

Before I go insane: Does anybody have an idea?


Fri Nov 03, 2017 9:23 pm
Profile
Site Admin

Joined: Tue Aug 09, 2005 3:21 am
Posts: 1210
Location: UK
Newlib based toolchains define int32_t as long, literal numbers with no suffix are int. Easiest solution is so use int in your templates instead of int32_t

_________________
Help keep devkitPro toolchains free, Donate today

devkitPro IRC support
Personal Blog


Sun Nov 05, 2017 4:12 pm
Profile ICQ WWW

Joined: Thu Feb 03, 2011 10:47 pm
Posts: 210
I made some changes and this seems to work:

Code:
#include <cstdint>
#include <iostream>

template <unsigned F> // changed F to unsigned
struct fp
{
  static_assert(F < 32); // assert shifts are valid

  constexpr static unsigned BITS = F; // change to constexpr static member instead of enum

  int32_t s; // to make sure it's exactly 32-bits wide on any platform

  // this gives the same result for both integral and floating-point types,
  // and the optimizer makes it as efficient as you had before with them separate
  template <typename T>
  fp(const T & b) : s(b * (1 << F)) { }

  // this was fine; delegates to operator=
  // this will be chosen over the previous template because it is more specific
  template<unsigned T>
  fp(const fp<T> & b) { *this = b; }

  // same reason as constructor; no need to have separate integral and floating-point
  // implementations
  template <typename T>
  fp & operator=(const T & b) { s = b * (1 << F); return *this; }

  // removed operator=(const fp &) since it's already covered by this
  template<unsigned T>
  fp & operator=(const fp<T> & b)
  {
    if (T > F) { s = b.s >> (T - F); }
    else if (T < F) { s = b.s << (F - T); }
    else { s = b.s; }
    return *this;
  }

  // removed template argument because it's already implied
  friend std::ostream & operator<<(std::ostream & os, const fp & a) { os << a.s; return os; }
};

using fp824 = fp<24>;
using fp1616 = fp<16>;
using fp248 = fp<8>;

int main() {
  // your code goes here
  fp1616 a0(0);
  fp1616 a1(1);
  fp1616 a2(2);
  fp1616 a3(-2.5f);
  std::cout << a0 << std::endl;
  std::cout << a1 << std::endl;
  std::cout << a2 << std::endl;
  std::cout << a3 << std::endl;
  a0 = 5;
  a0 = 0;
  a0 = 3.4f;
  fp824 b1(1.2f);
  fp1616 b2(b1);
  fp248 b3(b2);
  b1 = b3;
  b1 = b2;
  return 0;
}


Sun Nov 05, 2017 11:20 pm
Profile

Joined: Fri Nov 03, 2017 6:56 pm
Posts: 2
Not sure if my previous answer got lost, so:
Thanks for the answers. It was indeed the int vs. int32_t problem. My solution looks something like this atm. I wasn't sure about the compilers capabilities, so the different overloads...


Wed Nov 08, 2017 9:46 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
  Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.
Get devkitPro at SourceForge.net. Fast, secure and Free Open Source software downloads