函数专门化基于if模板参数是shared_ptr

 _i逗比_985 发布于 2023-02-13 19:04

我正在尝试检测类型是否为a shared_ptr,如果是,则调度到特定的函数模板或覆盖.

这是我实际尝试的简化版本:

#include 
#include 
#include 

template  struct is_shared_ptr : std::false_type {};
template  struct is_shared_ptr > : std::true_type {};

class Foo { };
typedef std::shared_ptr SharedFoo;

template void getValue();

template::value>::type = 0>
void getValue()
{
    printf("shared!\n");
}

template::value>::type = 0>
void getValue()
{
    printf("not shared!\n");
}

int main(int, char **)
{
    getValue();
    getValue();
    return 0;
}

它编译得很好,但似乎实际的函数从未实际生成,因为代码没有链接以下错误:

/tmp/ccjAKSBE.o: In function `main':
shared_test.cpp:(.text+0x10): undefined reference to `void getValue>()'
shared_test.cpp:(.text+0x15): undefined reference to `void getValue()'
collect2: error: ld returned 1 exit status

我认为这些将由两个功能模板涵盖.但他们不是.

鉴于此,我似乎对某些事情产生了严重的误解.

所以,如果我解释一下我在/尝试/做什么而不是我实际在做什么,也许会有所帮助.

我有一些"魔术"代码使用一堆新的(对我来说)C++ 11特性将C++代码绑定到lua(可以在这里看到:https://github.com/Tomasu/LuaGlue).有人最近要求支持绑定到包含在其中shared_ptr的类.这不是目前可用的东西,因为它在编译时使用模板和元组解包绑定生成代码以在C++或lua端调用函数.在"神奇"的展开代码中,我有一堆被覆盖的"专用"函数来处理各种变量类型.一些用于基本类型,一个用于静态对象,另一个用于指向对象.A shared_ptr不能以与静态或指针对象相同的方式处理,因此我需要为它们添加一些额外的处理.

例如:

template
T getValue(LuaGlue &, lua_State *, unsigned int);

template<>
int getValue(LuaGlue &, lua_State *state, unsigned int idx)
{
    return luaL_checkint(state, idx);
}

template
T getValue(LuaGlue &g, lua_State *state, unsigned int idx)
{
    return getValue_(g, state, idx, std::is_pointer());
}

这是实际的代码(通过函数参数注意毛茸茸的模板/覆盖:-x).

我原以为它就像添加另一个addValue函数一样简单,就像我之前的例子中的代码一样enable_if.

1 个回答
  • 有什么理由不简单地使用部分专业化?

    #include <type_traits>
    #include <memory>
    #include <cstdio>
    
    class Foo { };
    typedef std::shared_ptr<Foo> SharedFoo;
    
    template <class T>
    struct getValue {
        getValue() {
            printf("not shared!\n");
        }
    };
    
    template <class T>
    struct getValue<std::shared_ptr<T> > {
        getValue() {
            printf("shared!\n");
        }
    };
    
    int main(int, char **)
    {
        getValue<SharedFoo>();
        getValue<Foo>();
        return 0;
    }
    

    2023-02-13 19:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有