Template argument deduction failed with typedef?











up vote
0
down vote

favorite












Considering the following couple of classes:



template <typename T1, typename T2>
class A{
public:
// ...
};

template<typename _T>
struct alias { typedef A<int,_T> intA; };

class B{
public:
// ...
template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
};


When I try to assign an object of class A<int,int> to an object of class B, I get the following compilation error:



template argument deduction/substitution failed: couldn't deduce template parameter ‘_T’



Is there an alternative way to use something of a typedef as the input argument to B::operator=()??










share|improve this question




















  • 3




    This isn't the problem, but names that begin with an underscore followed by a capital letter (_T) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code.
    – Pete Becker
    Nov 10 at 19:20















up vote
0
down vote

favorite












Considering the following couple of classes:



template <typename T1, typename T2>
class A{
public:
// ...
};

template<typename _T>
struct alias { typedef A<int,_T> intA; };

class B{
public:
// ...
template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
};


When I try to assign an object of class A<int,int> to an object of class B, I get the following compilation error:



template argument deduction/substitution failed: couldn't deduce template parameter ‘_T’



Is there an alternative way to use something of a typedef as the input argument to B::operator=()??










share|improve this question




















  • 3




    This isn't the problem, but names that begin with an underscore followed by a capital letter (_T) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code.
    – Pete Becker
    Nov 10 at 19:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Considering the following couple of classes:



template <typename T1, typename T2>
class A{
public:
// ...
};

template<typename _T>
struct alias { typedef A<int,_T> intA; };

class B{
public:
// ...
template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
};


When I try to assign an object of class A<int,int> to an object of class B, I get the following compilation error:



template argument deduction/substitution failed: couldn't deduce template parameter ‘_T’



Is there an alternative way to use something of a typedef as the input argument to B::operator=()??










share|improve this question















Considering the following couple of classes:



template <typename T1, typename T2>
class A{
public:
// ...
};

template<typename _T>
struct alias { typedef A<int,_T> intA; };

class B{
public:
// ...
template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
};


When I try to assign an object of class A<int,int> to an object of class B, I get the following compilation error:



template argument deduction/substitution failed: couldn't deduce template parameter ‘_T’



Is there an alternative way to use something of a typedef as the input argument to B::operator=()??







c++ class templates typedef






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 19:30

























asked Nov 10 at 19:12









joaocandre

4751022




4751022








  • 3




    This isn't the problem, but names that begin with an underscore followed by a capital letter (_T) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code.
    – Pete Becker
    Nov 10 at 19:20














  • 3




    This isn't the problem, but names that begin with an underscore followed by a capital letter (_T) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code.
    – Pete Becker
    Nov 10 at 19:20








3




3




This isn't the problem, but names that begin with an underscore followed by a capital letter (_T) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code.
– Pete Becker
Nov 10 at 19:20




This isn't the problem, but names that begin with an underscore followed by a capital letter (_T) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code.
– Pete Becker
Nov 10 at 19:20












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










templated using might fix the issue



template <typename T1, typename T2>
class A{
public:
// ...
};

template<typename _T>
using alias = A<int,_T>;

class B{
public:
// ...
template <typename _T> B& operator=(const alias<_T>& ) { return *this; };
};

void f()
{
B b;
A<int, int> a;
b = a;
}





share|improve this answer




























    up vote
    1
    down vote













    The problem is that intA is a dependant name. Templates cannot be deduced from dependant names. See for example: Dependent Types: Template argument deduction failed.



    You are also missing the typename keyword.



    You can either explicitly specify the type for the operator:



    template <typename T1, typename T2>
    struct A{ };

    template<typename _T>
    struct alias { typedef A<int,_T> intA; };

    struct B
    {
    template <typename T> B& operator=(const typename alias<T>::intA& _arg) { };
    };

    int main()
    {
    A<int,int> a;
    B b;
    b.operator=<int>(a);
    return 0;
    }


    or you can have a specific, non-dependant-name parameter using a templated alias (with or without a function):



    template <typename T1, typename T2>
    struct A{ };

    template<class T>
    using alias_int = A<int, T>;

    struct alias
    {
    template<class T>
    using intA = A<int, T>;
    };

    struct B
    {
    template <typename T> B& operator=(const alias_int<T>& _arg) { };
    };

    struct C
    {
    template <typename T> C& operator=(const alias::intA<T>& _arg) { };
    };

    int main()
    {
    A<int,int> a;
    B b;
    C c;
    b = a;
    c = a;
    return 0;
    }





    share|improve this answer






























      up vote
      0
      down vote













      I'm getting a different error (using g++ 5.4):
      need ‘typename’ before ‘alias<_T>::intA’ because ‘alias<_T>’ is a dependent scope
      and true enough the following compiles for me:



      template <typename T1, typename T2>
      class A{
      public:
      // ...
      };

      template<typename _T>
      struct alias { typedef A<int,_T> intA; };

      class B{
      public:
      // ...
      template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
      };


      I think the reason is that alias<_T>::intA isn't an actual type but a templated typename.






      share|improve this answer





















      • I forgot the typename keyword on my code sample. I'm attemping to compile with -std=c++11 if that's relevant.
        – joaocandre
        Nov 10 at 19:31










      • Compiles for me with -std=c++11.
        – Henning Koehler
        Nov 10 at 19:34











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242504%2ftemplate-argument-deduction-failed-with-typedef%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      templated using might fix the issue



      template <typename T1, typename T2>
      class A{
      public:
      // ...
      };

      template<typename _T>
      using alias = A<int,_T>;

      class B{
      public:
      // ...
      template <typename _T> B& operator=(const alias<_T>& ) { return *this; };
      };

      void f()
      {
      B b;
      A<int, int> a;
      b = a;
      }





      share|improve this answer

























        up vote
        2
        down vote



        accepted










        templated using might fix the issue



        template <typename T1, typename T2>
        class A{
        public:
        // ...
        };

        template<typename _T>
        using alias = A<int,_T>;

        class B{
        public:
        // ...
        template <typename _T> B& operator=(const alias<_T>& ) { return *this; };
        };

        void f()
        {
        B b;
        A<int, int> a;
        b = a;
        }





        share|improve this answer























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          templated using might fix the issue



          template <typename T1, typename T2>
          class A{
          public:
          // ...
          };

          template<typename _T>
          using alias = A<int,_T>;

          class B{
          public:
          // ...
          template <typename _T> B& operator=(const alias<_T>& ) { return *this; };
          };

          void f()
          {
          B b;
          A<int, int> a;
          b = a;
          }





          share|improve this answer












          templated using might fix the issue



          template <typename T1, typename T2>
          class A{
          public:
          // ...
          };

          template<typename _T>
          using alias = A<int,_T>;

          class B{
          public:
          // ...
          template <typename _T> B& operator=(const alias<_T>& ) { return *this; };
          };

          void f()
          {
          B b;
          A<int, int> a;
          b = a;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 19:22









          Alexander

          332110




          332110
























              up vote
              1
              down vote













              The problem is that intA is a dependant name. Templates cannot be deduced from dependant names. See for example: Dependent Types: Template argument deduction failed.



              You are also missing the typename keyword.



              You can either explicitly specify the type for the operator:



              template <typename T1, typename T2>
              struct A{ };

              template<typename _T>
              struct alias { typedef A<int,_T> intA; };

              struct B
              {
              template <typename T> B& operator=(const typename alias<T>::intA& _arg) { };
              };

              int main()
              {
              A<int,int> a;
              B b;
              b.operator=<int>(a);
              return 0;
              }


              or you can have a specific, non-dependant-name parameter using a templated alias (with or without a function):



              template <typename T1, typename T2>
              struct A{ };

              template<class T>
              using alias_int = A<int, T>;

              struct alias
              {
              template<class T>
              using intA = A<int, T>;
              };

              struct B
              {
              template <typename T> B& operator=(const alias_int<T>& _arg) { };
              };

              struct C
              {
              template <typename T> C& operator=(const alias::intA<T>& _arg) { };
              };

              int main()
              {
              A<int,int> a;
              B b;
              C c;
              b = a;
              c = a;
              return 0;
              }





              share|improve this answer



























                up vote
                1
                down vote













                The problem is that intA is a dependant name. Templates cannot be deduced from dependant names. See for example: Dependent Types: Template argument deduction failed.



                You are also missing the typename keyword.



                You can either explicitly specify the type for the operator:



                template <typename T1, typename T2>
                struct A{ };

                template<typename _T>
                struct alias { typedef A<int,_T> intA; };

                struct B
                {
                template <typename T> B& operator=(const typename alias<T>::intA& _arg) { };
                };

                int main()
                {
                A<int,int> a;
                B b;
                b.operator=<int>(a);
                return 0;
                }


                or you can have a specific, non-dependant-name parameter using a templated alias (with or without a function):



                template <typename T1, typename T2>
                struct A{ };

                template<class T>
                using alias_int = A<int, T>;

                struct alias
                {
                template<class T>
                using intA = A<int, T>;
                };

                struct B
                {
                template <typename T> B& operator=(const alias_int<T>& _arg) { };
                };

                struct C
                {
                template <typename T> C& operator=(const alias::intA<T>& _arg) { };
                };

                int main()
                {
                A<int,int> a;
                B b;
                C c;
                b = a;
                c = a;
                return 0;
                }





                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The problem is that intA is a dependant name. Templates cannot be deduced from dependant names. See for example: Dependent Types: Template argument deduction failed.



                  You are also missing the typename keyword.



                  You can either explicitly specify the type for the operator:



                  template <typename T1, typename T2>
                  struct A{ };

                  template<typename _T>
                  struct alias { typedef A<int,_T> intA; };

                  struct B
                  {
                  template <typename T> B& operator=(const typename alias<T>::intA& _arg) { };
                  };

                  int main()
                  {
                  A<int,int> a;
                  B b;
                  b.operator=<int>(a);
                  return 0;
                  }


                  or you can have a specific, non-dependant-name parameter using a templated alias (with or without a function):



                  template <typename T1, typename T2>
                  struct A{ };

                  template<class T>
                  using alias_int = A<int, T>;

                  struct alias
                  {
                  template<class T>
                  using intA = A<int, T>;
                  };

                  struct B
                  {
                  template <typename T> B& operator=(const alias_int<T>& _arg) { };
                  };

                  struct C
                  {
                  template <typename T> C& operator=(const alias::intA<T>& _arg) { };
                  };

                  int main()
                  {
                  A<int,int> a;
                  B b;
                  C c;
                  b = a;
                  c = a;
                  return 0;
                  }





                  share|improve this answer














                  The problem is that intA is a dependant name. Templates cannot be deduced from dependant names. See for example: Dependent Types: Template argument deduction failed.



                  You are also missing the typename keyword.



                  You can either explicitly specify the type for the operator:



                  template <typename T1, typename T2>
                  struct A{ };

                  template<typename _T>
                  struct alias { typedef A<int,_T> intA; };

                  struct B
                  {
                  template <typename T> B& operator=(const typename alias<T>::intA& _arg) { };
                  };

                  int main()
                  {
                  A<int,int> a;
                  B b;
                  b.operator=<int>(a);
                  return 0;
                  }


                  or you can have a specific, non-dependant-name parameter using a templated alias (with or without a function):



                  template <typename T1, typename T2>
                  struct A{ };

                  template<class T>
                  using alias_int = A<int, T>;

                  struct alias
                  {
                  template<class T>
                  using intA = A<int, T>;
                  };

                  struct B
                  {
                  template <typename T> B& operator=(const alias_int<T>& _arg) { };
                  };

                  struct C
                  {
                  template <typename T> C& operator=(const alias::intA<T>& _arg) { };
                  };

                  int main()
                  {
                  A<int,int> a;
                  B b;
                  C c;
                  b = a;
                  c = a;
                  return 0;
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 19:48

























                  answered Nov 10 at 19:31









                  Pixelchemist

                  16k43162




                  16k43162






















                      up vote
                      0
                      down vote













                      I'm getting a different error (using g++ 5.4):
                      need ‘typename’ before ‘alias<_T>::intA’ because ‘alias<_T>’ is a dependent scope
                      and true enough the following compiles for me:



                      template <typename T1, typename T2>
                      class A{
                      public:
                      // ...
                      };

                      template<typename _T>
                      struct alias { typedef A<int,_T> intA; };

                      class B{
                      public:
                      // ...
                      template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
                      };


                      I think the reason is that alias<_T>::intA isn't an actual type but a templated typename.






                      share|improve this answer





















                      • I forgot the typename keyword on my code sample. I'm attemping to compile with -std=c++11 if that's relevant.
                        – joaocandre
                        Nov 10 at 19:31










                      • Compiles for me with -std=c++11.
                        – Henning Koehler
                        Nov 10 at 19:34















                      up vote
                      0
                      down vote













                      I'm getting a different error (using g++ 5.4):
                      need ‘typename’ before ‘alias<_T>::intA’ because ‘alias<_T>’ is a dependent scope
                      and true enough the following compiles for me:



                      template <typename T1, typename T2>
                      class A{
                      public:
                      // ...
                      };

                      template<typename _T>
                      struct alias { typedef A<int,_T> intA; };

                      class B{
                      public:
                      // ...
                      template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
                      };


                      I think the reason is that alias<_T>::intA isn't an actual type but a templated typename.






                      share|improve this answer





















                      • I forgot the typename keyword on my code sample. I'm attemping to compile with -std=c++11 if that's relevant.
                        – joaocandre
                        Nov 10 at 19:31










                      • Compiles for me with -std=c++11.
                        – Henning Koehler
                        Nov 10 at 19:34













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      I'm getting a different error (using g++ 5.4):
                      need ‘typename’ before ‘alias<_T>::intA’ because ‘alias<_T>’ is a dependent scope
                      and true enough the following compiles for me:



                      template <typename T1, typename T2>
                      class A{
                      public:
                      // ...
                      };

                      template<typename _T>
                      struct alias { typedef A<int,_T> intA; };

                      class B{
                      public:
                      // ...
                      template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
                      };


                      I think the reason is that alias<_T>::intA isn't an actual type but a templated typename.






                      share|improve this answer












                      I'm getting a different error (using g++ 5.4):
                      need ‘typename’ before ‘alias<_T>::intA’ because ‘alias<_T>’ is a dependent scope
                      and true enough the following compiles for me:



                      template <typename T1, typename T2>
                      class A{
                      public:
                      // ...
                      };

                      template<typename _T>
                      struct alias { typedef A<int,_T> intA; };

                      class B{
                      public:
                      // ...
                      template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
                      };


                      I think the reason is that alias<_T>::intA isn't an actual type but a templated typename.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 10 at 19:28









                      Henning Koehler

                      1,095510




                      1,095510












                      • I forgot the typename keyword on my code sample. I'm attemping to compile with -std=c++11 if that's relevant.
                        – joaocandre
                        Nov 10 at 19:31










                      • Compiles for me with -std=c++11.
                        – Henning Koehler
                        Nov 10 at 19:34


















                      • I forgot the typename keyword on my code sample. I'm attemping to compile with -std=c++11 if that's relevant.
                        – joaocandre
                        Nov 10 at 19:31










                      • Compiles for me with -std=c++11.
                        – Henning Koehler
                        Nov 10 at 19:34
















                      I forgot the typename keyword on my code sample. I'm attemping to compile with -std=c++11 if that's relevant.
                      – joaocandre
                      Nov 10 at 19:31




                      I forgot the typename keyword on my code sample. I'm attemping to compile with -std=c++11 if that's relevant.
                      – joaocandre
                      Nov 10 at 19:31












                      Compiles for me with -std=c++11.
                      – Henning Koehler
                      Nov 10 at 19:34




                      Compiles for me with -std=c++11.
                      – Henning Koehler
                      Nov 10 at 19:34


















                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242504%2ftemplate-argument-deduction-failed-with-typedef%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python