type-deduction

Template Alias, Variable Template, and auto type deduction failing to deduce template argument

安稳与你 提交于 2019-12-24 19:35:47
问题 While working on my class declaration I'm having some confusion on how to use alias templates and template variables within in a non class template while trying to use auto type deduction. Signal.h #ifndef SIGNAL_H #define SIGNAL_H #include <cstdint> template<typename T> using TimeSignal = T; using DiscreteTime = TimeSignal<std::uint8_t>; using ContinuousTime = TimeSignal<double>; class Signal { private: template<typename T> static TimeSignal<T> time_; double voltage_; double current_; public

How does the type deduction work in this Docopt example?

回眸只為那壹抹淺笑 提交于 2019-12-24 01:18:00
问题 Take a look at this code using the docopt library: const USAGE: &'static str = "...something..."; #[derive(Deserialize)] struct Args { flag: bool, } type Result<T> = result::Result<T, Box<error::Error + Send + Sync>>; fn main() { let mut args: Args = Docopt::new(USAGE) .and_then(|d| d.deserialize()) .unwrap_or_else(|e| e.exit()); } If you look at the expression to the right of equals sign, you'll see that it doesn't mention the Args struct anywhere. How does the compiler deduce the return

Lambda did not automatically deduce return type

♀尐吖头ヾ 提交于 2019-12-23 16:34:38
问题 When I answewred my own question on https://stackoverflow.com/a/32115498/383779 , I got another doubt. In const CArray<CItem*>& Items= (ItemsInput!= nullptr)? *ItemsInput : [this]() -> const CArray<CItem*>& { CArray<CItem*> InnerItems; GetContainer().GetInnerItems(InnerItems, NULL, true); return (InnerItems); } () ; I tried to remove the -> const CArray<CItem*>& return part, but it gave two errors when compiling: 1>FunctionClass.cpp(line of last semicolon): error C2440: 'initializing' :

What are the type deduction rules for auto*?

北慕城南 提交于 2019-12-21 21:35:48
问题 What are the type deduction rules for auto* ? Consider the following: int x = 64; int* px = &x; auto* v1 = &x; // auto => ??? ok v1 is int* ... auto* v2 = px; // auto => ??? is v2 int* ? auto* v3 = &px; // auto => ??? is v3 int** ? Just to clarify my question if we split the type deduction into two steps: Deducing the type of " auto " itself without ( * ) ... then Deducing the type of the object ( v1 , v2 and v3 ) after adding the ( * ) So my two questions are: What will auto be deduced to

Why type-deduction for arrays prioritizes pointer to first over reference to array?

。_饼干妹妹 提交于 2019-12-21 11:45:08
问题 int v[1]; auto p1 = v; auto &p2 = v; auto *p3 = v; p1 is of type int * (same for p3 ). Particularly at this trivial sample I find p2 ( int (&)[1] ) more useful since it inherents array semantics, e.g. I can apply sizeof on p2 to give the same as sizeof on v . Is there a standard quotation regarding that? Why defaulting to references is a bad idea? (for this arrays case I mean, almost no c++ programmer cares about them these days anyway...) 回答1: auto deduces a non-reference type. auto& deduces

Forwarding arguments to template member function

点点圈 提交于 2019-12-21 07:34:04
问题 ideone example I need to forward some predefined arguments plus some user-passed arguments to a member function. #define FWD(xs) ::std::forward<decltype(xs)>(xs) template<class T, class... Ts, class... TArgs> void forwarder(void(T::*fptr)(Ts...), TArgs&&... xs) { T instance; (instance.*fptr)(FWD(xs)..., 0); // ^ // example predefined argument } forwarder(&example::f0, 10, 'a'); forwarder(&example::f1, 10, "hello", 5); This works properly for non-template member functions. The member function

What does T::* mean in template's parameters?

天大地大妈咪最大 提交于 2019-12-19 11:40:55
问题 Following the article written in here: I came across this code (shortened and changed for clarity): template <class T> struct hasSerialize { // This helper struct permits us to check that serialize is truly a method. // The second argument must be of the type of the first. // For instance reallyHas<int, 10> would be substituted by reallyHas<int, int 10> and works! // reallyHas<int, &C::serialize> would be substituted by reallyHas<int, int &C::serialize> and fail! // Note: It only works with

Strong typedefs [duplicate]

主宰稳场 提交于 2019-12-17 16:34:37
问题 This question already has answers here : C++ Strongly typed using and typedef (4 answers) Closed 2 years ago . Is there any way to make a complete copy of a type so that they can be distinguished in template deduction context? Take the example: #include <iostream> template <typename T> struct test { static int c() { static int t = 0; return t++; } }; typedef int handle; int main() { std::cout << test<int>::c() << std::endl; std::cout << test<handle>::c() << std::endl; return 0; } Since

Failed template argument deduction when using template template parameters

*爱你&永不变心* 提交于 2019-12-13 02:51:09
问题 I wanted to create a simple auxiliary algorithm that would fill a container, such as std::vector<T> , with a geometric progression (the first term is a , and the n -th term is given by a * pow(r, n-1) , where r is a given ratio); I created the following code: #include<vector> #include<algorithm> #include<iostream> template<template <typename> class Container, typename T> void progression(Container<T>& container, T a, T ratio, size_t N) { if(N > 0) { T factor = T(1); for(size_t k=0; k<N; k++)

Deduce argument type in template parametrized by function

被刻印的时光 ゝ 提交于 2019-12-12 00:08:35
问题 I have C framework that provides classic deferred callback execution API: typedef void (*callback_t)(int value_1, int value_2, void* arg); void schedule(callback_t callback, void* arg); Here arg is arbitrary value specified by user that will be passed to the callback and other callback parameters are passed by framework. But really only last argument is used in handlers and it has to be explicitly cast from void* . Thinking of how callback signature may be changed to match the real one I got