logo
down
shadow

Failing to specialize function for function pointer


Failing to specialize function for function pointer

By : FrancoisT
Date : November 20 2020, 03:01 PM
may help you . None of this works, because substitution failure is only a failure in the immediate context of the substitution - and you're trying to SFINAE in a context that doesn't depend on the immediate function template parameters. Your constraints in func() are based on the class template parameters, not the local function template parameters, so those are just a hard error.
The easiest way to do this is via tag dispatching. Wrap decltype(mOp()) into a tag type and then just overload:
code :
template <class T> struct tag { };

template <typename T>
struct S
{
    T mOp;

    void func() {
        func_impl(tag<std::decay_t<decltype(mOp())>>{});
    }

    void func_impl(tag<bool> ) { std::cout << "bool\n"; }        
    void func_impl(tag<void> ) { std::cout << "void\n"; }        
};
template <class..., class U=T>
auto func()
    -> decltype(func_impl(tag<std::decay_t<std::invoke_result_t<U>>>{}))
{
    return func_impl(tag<std::decay_t<std::invoke_result_t<U>>>{});
}


Share : facebook icon twitter icon
Specialize a template class with a member function pointer

Specialize a template class with a member function pointer


By : Mike Hudson
Date : March 29 2020, 07:55 AM
this one helps. If I do understand correctly what you are trying to achieve, it cannot be done. Let's consider an even simpler case of illegal specialization:
code :
template <typename T1, typename T2, void (T1::* FUNC)(const T2&)> struct list;
template <typename T1, typename T2> struct list<T1, T2, 0> { };
template <class T, T t> struct C {};
template <class T> struct C<T, 1>; // error

template< int X, int (*array_ptr)[X] > class A {};
int array[5];
template< int X > class A<X,&array> { }; // error
Is there a way to specialize a templated function to discriminate when the type of an argument is a pointer from when it

Is there a way to specialize a templated function to discriminate when the type of an argument is a pointer from when it


By : matt
Date : March 29 2020, 07:55 AM
this one helps. You just need to swap the functions around. In template void Func(T* p) when you do Func(*p); all the compiler knows about is template void Func(T* p). So you wind up calling the same function recursively until it errors when you no longer have a pointer. If you use
code :
template <typename T> void Func(T)
{
  printf("type");
}

template <typename T> void Func(T* p)
{
  printf("pointer ");
  Func(*p);
}
pointer pointer pointer type
Compile time specialize against function pointer references for avoiding -Waddress

Compile time specialize against function pointer references for avoiding -Waddress


By : Kail
Date : March 29 2020, 07:55 AM
hope this fix your issue I'm encountering the following issue with GCC (tested with v6.4 it's the same behavior on the current trunk) which can be reduced to the following minimalistic example:
code :
std::decay_t<O>
template <typename Ret, typename... Args>
struct invoke_trait<Ret (*)(Args...)> {
  template <typename O>
  static void invoke(O&& fn) {
    if (fn) {
      std::invoke(std::forward<O>(fn));
    }
  }
};

template <typename Ret, typename... Args>
struct invoke_trait<Ret(Args...)> {
  template <typename O>
  static void invoke(O&& fn) {
    std::invoke(std::forward<O>(fn));
  }
};

template <typename O>
void safe_invoke(O&& fn) {
  using trait_t = invoke_trait<std::remove_cv_t<std::remove_reference_t<O>>>;
  trait_t::invoke(std::forward<O>(fn));
}
How to partially specialize a struct template for non-member & member function pointer

How to partially specialize a struct template for non-member & member function pointer


By : user3328015
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I'm doing some template metaprogramming involving function pointers. Because the function types for non-member and member function pointers are different, I'm trying to specialize between the two. Furthermore, I want the pointer to the function to be provided as a non-type template argument instead of a parameter to a function in the struct. So far here's what I have attempted: , Here's how you can fix the code:
code :
template <typename T, T>
struct register_method;

template <typename R, typename... Args, R (*method)(Args...)>
struct register_method<R (*)(Args...), method>
{
    template <typename ...P>
    static R invoke(P &&... params)
    {
        return (*method)(std::forward<P>(params)...);
    }
};

template <typename T, typename R, typename... Args, R (T::*method)(Args...)>
struct register_method<R (T::*)(Args...), method>
{
    template <typename ...P>
    static R invoke(T *instance, P &&... params)
    {
        return (instance->*method)(std::forward<P>(params)...);
    }
};
template <auto method>
struct register_method;

template <typename R, typename... Args, R (*method)(Args...)>
struct register_method<method>
{
    template <typename ...P>
    static R invoke(P &&... params)
    {
        return (*method)(std::forward<P>(params)...);
    }
};

template <typename T, typename R, typename... Args, R (T::*method)(Args...)>
struct register_method<method>
{
    template <typename ...P>
    static R invoke(T *instance, P &&... params)
    {
        return (instance->*method)(std::forward<P>(params)...);
    }
};
Specialize C++ template function that uses generic iterators for pointer value type?

Specialize C++ template function that uses generic iterators for pointer value type?


By : Sujit Sakre
Date : March 29 2020, 07:55 AM
Hope this helps It is easier to write a template functor to update a single item, and specialize that. Then, have your update template function invoke the class. Something like:
code :
template <typename T>
struct Update {
    void operator () (const T &v) {
        //...
    }
};

template <typename T>
struct Update<T *> {
    void operator () (const T *v) {
        //...
    }
};


template <typename T>
void update (const T &v) {
    Update<T>()(v);
}
update(*iter);
template <typename T, typename U = Update<T>>
void update (const T &v, U u = U{}) {
    u(v);
}
update(*iter, [](int *){ /* ... */ });
Related Posts Related Posts :
  • Copy and Base Constructor don't initialize C++
  • Xcode gives Mach-O linker error: "_JNI_CreateJavaVM", referenced from: _main in main.o
  • Variadic deduction guide not taken by g++, taken by clang++ - who is correct?
  • Write a proxy class in C++
  • Qt testing when dependent on Network
  • RGB color in 24 bitmap image and 16 bitmap image
  • shadertoy to SFML
  • Inserting Objects into an array with dynamic memory (No vectors allowed) C++
  • Find parent node function for binary tree
  • Rotate Binary Tree
  • Boost asio async_read_until stop reading on matched condition
  • How can I change the QGraphicView selected Item color
  • Understanding object slicing
  • How to change pointer so that it point to somewhere else?
  • Dynamically add overlay widgets
  • Compressing 3 number data in a byte
  • OpenGL - Uniform not being passed properly?
  • C++ Visual Studio "Non-standard syntax; use '&' to create a pointer to member"
  • Doxygen won't resolve reference to typedef in namespace
  • How to test for assert in debug build, but returned value in release?
  • use boost spirit parse int pair to vector
  • How #define works in Programming when define has value with operator?
  • C++: Passing objects by value to a member function of the same class
  • Confusion in C++ constructor
  • Implicit conversion operator doesn't kick in with operator overloading
  • From tuple of N elements to tuple of N/2 pairs
  • Operators Not Working With Class Objects?
  • GDAL Not Linking
  • C++ aligning characters to display neatly with any input implemented
  • c++, method to display content in hash table.Using double linked list
  • How to apply a function to a variadic argument list and cat them to a tuple
  • Doubly linked list c++ crashing after 3rd node
  • Transform a specific type of argument using templates
  • Function with recursion is causing stack overflow
  • working with multi-word typedefs
  • Calling std::to_string or std::to_wstring based on given typedef
  • Ensure a `std::vector` won't move its pointer
  • Appopriate Container that acts like normal static array?
  • Using enable_if to disable a template constructor of a template class
  • define implicit conversion from template class to primitive type based on other template
  • C++ class template, how to overload [ ] operator in a specific stuation?
  • What is the purpose of using an iterator in this function's return value?
  • C++ overriding the value of inherited static const variables
  • C++: what is special about punning character arrays according to the standard?
  • Producer consumer pthreads program not finishing
  • How do I start two thread teams with different thread functions at the same time?
  • OpenGL 3.3: GL_INVALID_OPERATION when calling glBindBuffer
  • Boost Karma generator for composition of classes
  • Can the storage of trivially copyable objects be safely reallocated with realloc?
  • Can I force a C++ class to be non-abstract?
  • The QPropertyAnimation is automatically deleted when using QParallelAnimationGroup?
  • Check if two (smart) pointers point to the same derived class
  • Getting an error when using delete[] cpp
  • recursion in variadic template function of different argument types
  • QTWidgets QTableWidget crash with segfault
  • clang and clang++ with ASAN generate different output
  • How to map BYTE array as FILE * on Windows
  • Logic Error With Function to Manipulate Linked List
  • Expected primary-expression before numeric constant
  • Pre-Decrement Operator
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org