标签: template-function

为什么ADL没有找到功能模板?

C++规范的哪一部分限制参数依赖查找在相关命名空间集合中查找函数模板?换句话说,为什么main下面的最后一次调用无法编译?

namespace ns {
    struct foo {};
    template<int i> void frob(foo const&) {}
    void non_template(foo const&) {}
}

int main() {
    ns::foo f;
    non_template(f); // This is fine.
    frob<0>(f); // This is not.
}
Run Code Online (Sandbox Code Playgroud)

c++ template-function name-lookup argument-dependent-lookup

79
推荐指数
3
解决办法
3825
查看次数

内联模板功能?

inline如果它们包含在多个cpp文件中,我是否需要模板功能?谢谢.

template<bool> inline QString GetText();
template<> inline QString GetText<true>() {return "true";}
template<> inline QString GetText<false>() {return "false";}
Run Code Online (Sandbox Code Playgroud)

c++ template-function

24
推荐指数
2
解决办法
2万
查看次数

为什么C++ 11没有隐式地将lambda转换为std :: function对象?

我实现了一个通用事件发射器类,它允许代码注册回调,并使用参数发出事件.我使用Boost.Any类型擦除来存储回调,以便它们可以具有任意参数签名.

这一切都有效,但由于某种原因,传入的lambdas必须首先变成std::function对象.为什么编译器不推断lambda是函数类型?是因为我使用可变参数模板的方式吗?

我使用Clang(版本字符串:) Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn).

码:

#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#include <boost/any.hpp>


using std::cout;
using std::endl;
using std::function;
using std::map;
using std::string;
using std::vector;


class emitter {

   public:

      template <typename... Args>
      void on(string const& event_type, function<void (Args...)> const& f) {
         _listeners[event_type].push_back(f);
      }

      template <typename... Args>
      void emit(string const& event_type, Args... args) {
         auto listeners = _listeners.find(event_type);
         for (auto l : listeners->second) {
            auto lf …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates template-function c++11

15
推荐指数
2
解决办法
2276
查看次数

std :: vector <T>的比较运算符无法找到T的比较运算符

以下非常简单的代码将无法编译

#include <vector>
#include <string>


namespace Foobar {
    struct Test {
        std::string f;
        std::uint16_t uuid;
    };
}

bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){
    return lhs.f == rhs.f && lhs.uuid == rhs.uuid;
}


int main(){

    std::vector<Foobar::Test> a;
    std::vector<Foobar::Test> b;

    if(a==b){

    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/g/zn6UgJ

不会编译我的任何编译器.

而以下

#include <vector>
#include <string>


namespace Foobar {
    struct Test {
        std::string f;
        std::uint16_t uuid;
    };

    bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){
        return lhs.f == rhs.f && lhs.uuid == rhs.uuid;
    }
}



int …
Run Code Online (Sandbox Code Playgroud)

c++ dependent-name template-function name-lookup argument-dependent-lookup

13
推荐指数
1
解决办法
371
查看次数

没有使用模板函数从`int`自动转换为`float`

经过多年的c ++编码,今天我被问到一个简单的问题,但实际上我找不到答案,所以我在这里.

除了想知道为什么会发生这个错误之外,我想知道如何通过修改模板函数来解决下面的错误(不改变main()函数)

template <class T>
T Add(T first, T second)
{
    return first + second;
}

int main()
{
    auto sample_1 = Add(1, 2); // Works
    auto sample_2 = Add(1.f, 2.f); // Works
    auto sample_3 = Add(1.f, 2); // Error: no instance matches the argument types: (double, int)
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ template-function c++11

12
推荐指数
2
解决办法
1118
查看次数

如何专门化std :: begin?

我正在尝试专注std::begin于自定义容器.我这样做是因为我想使用基于范围for的容器.这就是我所拥有的:

class stackiterator { … };
class stack { … };

#include <iterator>

template <> stackiterator std::begin(stack& S)
{
  return S.GetBottom();
}
Run Code Online (Sandbox Code Playgroud)

我在我的begin专业化定义中遇到以下错误:

没有函数模板匹配函数模板特化"开始"

我究竟做错了什么?

c++ templates template-specialization template-function c++11

10
推荐指数
1
解决办法
3021
查看次数

C++:如何使用模板函数中的类型进行分支?

我对模板不太熟悉.如何编写一个名为get的模板函数,根据模板类型选择从中获取的数组?请参阅以下示例:

struct Foo
{
    int iArr[10];
    char cArr[10];

    // How to pick array here based on template type?
    template < typename T >
    T get( int idx )
    {
        // This does NOT work!
        switch ( T )
        {
        case int:
            return iArr[ idx ];
        case char:
            return cArr[ idx ];
        }
    }
};

// Expected behaviour of get()
Foo foo;
int i  = foo.get< int >( 2 );
char c = foo.get< char >( 4 );
Run Code Online (Sandbox Code Playgroud)

c++ template-function

9
推荐指数
2
解决办法
4215
查看次数

C++:候选模板被忽略:模板参数的显式指定参数无效

我有这个函数头:

template <
    bool src_alpha,
    int sbpp, int dbpp,
    typename T1, typename T2,
    Color (*getFunc)(T1 data, Uint8* addr),
    void (*putFunc)(T2 data, Uint8* addr, Color c)
>
static void OperateOnSurfaces(T1 data1, T2 data2, SDL_Surface * bmpDest, SDL_Surface * bmpSrc, SDL_Rect& rDest, SDL_Rect& rSrc)
Run Code Online (Sandbox Code Playgroud)

这就是我使用它的方式:

OperateOnSurfaces<
    true,
    32, 32,
    SDL_PixelFormat*, SDL_PixelFormat*,
    GetPixel<true,32>, PutPixel<true,true,32> >(
    bmpSrc->format, bmpDest->format,
    bmpDest, bmpSrc, rDest, rSrc);
Run Code Online (Sandbox Code Playgroud)

这是GetPixelPutPixel:

template<bool alpha, int bpp>
static Color GetPixel(SDL_PixelFormat* format, Uint8* addr) { /* .. */ }

template<bool alpha, …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-function

8
推荐指数
1
解决办法
8362
查看次数

为什么std :: swap不使用swap idiom?

正确使用std :: swap是:

using std::swap;
swap(a,b);
Run Code Online (Sandbox Code Playgroud)

它有点冗长,但它确保如果a,b有更好的交换定义它将被选中.

所以现在我的问题是为什么std::swap没有使用这种技术实现,所以用户代码只需要调用std::swap

所以这样的事情(noexcept为了简洁而忽略和约束):

namespace std {
    namespace internal {
        template <class T>      // normal swap implementation
        void swap(T& a, T& b) { // not intended to be called directly
            T tmp = std::move(a);
            a = std::move(b);
            b = std::move(tmp);
        }
    }

    template <class T>
    void swap(T& a, T& b) {
        using internal::swap;
        swap(a,b);
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ template-function argument-dependent-lookup customization-point

8
推荐指数
1
解决办法
340
查看次数

为什么std :: is_same为这两种类型提供了不同的结果?

在下面的代码中,为什么有两种调用方式fun:fun(num)fun<const int>(num)编译时给出不同的结果?

#include <iostream>
using namespace std;

template<typename T, typename = typename enable_if<!std::is_same<int, T>::value>::type>
void fun(T val)
{
    cout << val << endl;
}

int main(void)
{
    const int num = 42;
    fun(num);  //ERROR!

    fun<const int>(num);  //Right

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates const template-function argument-deduction

8
推荐指数
1
解决办法
386
查看次数