-3
#include <vector>

template<class T>
using vector_size = decltype(&(std::vector<T>::size));
//compiles fine

template<class T>
using vector_emplace_back = decltype(&(std::vector<T>::emplace_back<T&&>)));
//error

int main() {}

I'm not 100% sure of the vocabulary, but when I try to specify template parameters for a method function pointer templated using statement, I get compiler errors.

g++

main.cpp:8:70: error: expected primary-expression before '&&' token
    8 | using vector_emplace_back = decltype(&(std::vector<T>::emplace_back<T&&>)));
      |                                                                      ^~
main.cpp:8:72: error: expected primary-expression before '>' token
    8 | using vector_emplace_back = decltype(&(std::vector<T>::emplace_back<T&&>)));
      |                                                                        ^
main.cpp:8:73: error: expected primary-expression before ')' token
    8 | using vector_emplace_back = decltype(&(std::vector<T>::emplace_back<T&&>)));
      |                                                                         ^
main.cpp:8:75: error: expected ';' before ')' token
    8 | using vector_emplace_back = decltype(&(std::vector<T>::emplace_back<T&&>)));
      |                                                                           ^
      |                                                                           ;

clang++

main.cpp:8:70: error: expected '(' for function-style cast or type construction
using vector_emplace_back = decltype(&(std::vector<T>::emplace_back<T&&>)));
                                                                    ~^
main.cpp:8:72: error: expected expression
using vector_emplace_back = decltype(&(std::vector<T>::emplace_back<T&&>)));
                                                                       ^
main.cpp:8:73: error: expected expression
using vector_emplace_back = decltype(&(std::vector<T>::emplace_back<T&&>)));
                                                                        ^
main.cpp:8:75: error: expected ';' after alias declaration
using vector_emplace_back = decltype(&(std::vector<T>::emplace_back<T&&>)));
                                                                          ^
                                                                          ;

Visual Studio C++

source_file.cpp(8): error C2059: syntax error: ')'

Is there any workaround for this?

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • 1
    You need the keyword `template`, and remove the superfluous `)`. `template using vector_emplace_back = decltype(&(std::vector::template emplace_back));`. – songyuanyao Mar 07 '21 at 03:34
  • Ah, completely forgot about the `template` keyword in places like this – Mooing Duck Mar 07 '21 at 03:36
  • [Clang](https://wandbox.org/permlink/l5Mxo4sH1L3X0iuj) gives quite clear message for it. – songyuanyao Mar 07 '21 at 03:37
  • worse, that fixes my MCVE, but not my real code :( – Mooing Duck Mar 07 '21 at 03:37
  • @songyuanyao: Not my clang :( http://coliru.stacked-crooked.com/a/2c2452c43931b235, but maybe they've improved that error message? That'd be good. – Mooing Duck Mar 07 '21 at 03:38
  • Why do you do something this odd? – ALX23z Mar 07 '21 at 03:49
  • Just curious, did you vote to close with the "Typo, No repro" reason? – cigien Mar 07 '21 at 16:33
  • @ALX23z: inlinable/optimizable callbacks http://coliru.stacked-crooked.com/a/63e758996406ec2e – Mooing Duck Mar 08 '21 at 16:57
  • @MooingDuck why don't you just forward a lambda? It is simple, easy, flexible, and everybody knows how to use it. Why write something unnecessary verbose and much less flexible? – ALX23z Mar 08 '21 at 21:11
  • Check out this link - https://en.cppreference.com/w/cpp/language/lambda what you try to do is a very outdated approach. – ALX23z Mar 08 '21 at 21:14
  • @ALX23z: My core class is super flexible and allows devs to do anything. But it also ends up annoying for devs to use, because they have to write all these methods and manage state, and 99% of the time they just call a member function. So I'm making a helper which can handle the member functions as a template type, making usage trivial. – Mooing Duck Mar 08 '21 at 21:31

1 Answers1

0

It isn't exactly answer to your question. I just point out that you wrote something very wrong in your code. It feels that you do not understand what your code does.

  template<class T>
  using vector_size = decltype(&std::vector<T>::size);

  template<class T>
  using vector_capacity = decltype(&std::vector<T>::capacity);

  static_assert(std::is_same_v<vector_capacity<int>, vector_size<int>>,"size is capacity");

This code passes static assert. Why? because this using vector_size_int = decltype(&std::vector<int>::size); is not pointer to the size method of vector but it is a member function pointer type. At least calling it vector_size is very wrong.

ALX23z
  • 4,456
  • 1
  • 11
  • 18